My current .htaccess file does a conditional internal redirect using quite a few RewriteRules to the contents of a sub-directory.
I am now wanting to ban all access to this sub-directory, unless they have been redirected by an internal redirect (RewriteRule).
What could I use to create this conditional statement?
Use the server variable {THE_REQUEST}, which examines the original request header sent by the browser, and is unaffected by internal rewrites (redirects are, by definition, external).
A typical request from the browser looks like this:
GET /subdir/file.ext HTTP/1.1
So, you can detect and redirect or forbid any direct access to the rewritten URL by using:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subdir/file\.ext\ HTTP/
RewriteRule ^subdir/file\.ext$ http://www.example.com/original_fileURL [R=301,L]
-or-
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /subdir/file\.ext\ HTTP/
RewriteRule ^subdir/file\.ext$ - [F]
The redirect method is better in that it is 'friendlier' to the user, and can also be used to update any search results that now include the subdirectory URL. These rules work for all HTTP methods, such as GET, HEAD, PUT, PROPFIND, etc. as matched by the "[A=Z]{3,9}" pattern.
Tags: Apache, Miscellaneous, Url Rewrite
Related Items:
This website follows the ideas of the No Nofollow, I Follow, DoFollow, No-NoFollow movement. Leave a helpful comment and you will get a link without NoFollow (U Comment, I Follow).
If you are looking for web hosting then I would recommend my current web host. They have been good to me, suiting both beginners and advanced users.
Using a transparent and trusted advertising network always helps to generate more revenue on a website. One of the networks I use achieves this more than the others.
If you would like to help pay the web hosting bill for this site, you can donate through my host
If you are looking for web hosting then I would recommend my current web host. They have been good to me, suiting both beginners and advanced users.
Using a transparent and trusted advertising network always helps to generate more revenue on a website. One of the networks I use achieves this more than the others.
If you would like to help pay the web hosting bill for this site, you can donate through my host