I have a php website and I’m using mod_rewrite to redirect all of the URL requests. Suppose I have a product.php script and I have a rule for it:
RewriteRule ^product/([0-9]+).*/?$ product.php?pid=$1 [L,NC]
RewriteRule ^category/([0-9]+).*/?$ category.php?cid=$1 [L,NC]
So far, this works great and when the user types: http://www.mysite.com/product/1 the page with the actual URL of http:/www.mysite.com/product.php?pid=1 will be shown to him.
However, I want to force the user to access my site, exclusively from the rules that I have provided. By that, I mean when the user types http:/www.mysite.com/something or even some correct URL like http:/www.mysite.com/product.php?pid=1 he should be redirected to the 404 error page. This can be a custom script or apache’s default 404 page.
Is that possible?
EDIT: As requested, I post my entire .htaccess file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /websitename
RewriteRule ^product/([0-9]+).*/?$ product.php?pid=$1 [L,NC]
RewriteRule ^category/([0-9]+).*/?$ category.php?cid=$1 [L,NC]
You could just add a catch all as the last possible rewrite and map it to 404.php (or whatever you wan’t).
Just place this in the bottom of your rewrites.
EDIT:
I tested this out myself (i usually don’t use rewrites this way):
And also found that the 404 would be hit everytime. Apparently this is expected behaviour of .htaccess. (https://stackoverflow.com/a/3642271/603184)
So turns out you need it to stop matching when it hits product/category.php, this can be accomplished via adding:
Before the 404 redirect. Resulting in: