.htaccess mod_rewrite will not skip RewriteRule with [S]
As FYI, I am using the following .htaccess file located at www.site.com/content/
When a user visits www.site.com/content/ login site , I want him to display content from www.site.com/content/ userlogin.php site (masked by rewrite, not redirected) - which I did SUCCESSFULLY like this :
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,L]
However, I would like to add the following: if they try to access www.site.com/content/ userlogin.php directly, I want them to be redirected to 404 at www.site.com/content/ error / 404.php
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,S=1,L]
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
With this in a .htaccess file like www.site.com/content/ login and www.site.com/content/ userlogin.php show www.site.com/content/ error / 404.php
source to share
At first S = 1 will not function as the L directive will do any further rewriting. It seems that the first RewriteRule is forcing Apache to go through the .htaccess rules one more time, so you need to know if the first rewrite happened. You can do this by setting an environment variable like this:
RewriteRule ^login/?$ /content/userlogin.php [E=DONE:true,NC,L]
So when the next redirect happens, the environment variable is actually rewritten to REDIRECT_<variable>
, and you can do a RewriteCond like this:
RewriteCond %{ENV:REDIRECT_DONE} !true
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
Hope it helps
source to share