Smith Smith - 3 months ago 12
Apache Configuration Question

Allow only specific words within URL by htaccess

I searched the entire of stack overflow but was unable to figure out this.

I want allow only specific words within URL by htaccess with an exception.

E.g: All URLs with none of these words: (big, red, dark) to return 404 error unless there is a

&
in URL.

examples:

e.g1:

http://www.example.com/7big6556/
Allowed

e.g2:

http://www.example.com/8green65533
Return 404

e.g3:

http://www.example.com/hi-green&nice
Allowed

Answer

I come with solution:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteCond %{REQUEST_URI} !^/.*\/
RewriteCond %{REQUEST_URI} !big
RewriteCond %{REQUEST_URI} !red
RewriteCond %{REQUEST_URI} !dark
RewriteCond %{REQUEST_URI} !\&
RewriteRule ^(.*)$ - [L,R=404]
</IfModule>