Syncrat - An article website that focuses on Music, Dating & Relationships and Business & Money. Learn about music, improve your dating or learn to make your money go further.

Detecting internal redirects in url rewrite

Question

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?

Answer

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: , ,

Related Items:

Comments / Replies

Add Comment / Reply

The address of your website or social networking page. Make sure that you include http://
You can not use HTML in this field. If you wish to add a URL, write the full URL including http://
 

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).

Web Hosting

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.

Advertising

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.

Donations

If you would like to help pay the web hosting bill for this site, you can donate through my host

Your Ad Here

Web Hosting

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.

Advertising

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.

Donations

If you would like to help pay the web hosting bill for this site, you can donate through my host