Forum Moderators: phranque

Message Too Old, No Replies

Folder redirect with file exclusion

50% there... =D

         

Mikroz

7:13 pm on Jun 8, 2010 (gmt 0)

10+ Year Member



Hi all,

I have created a new folder on my server with some image files in it.

/subfolder/

I am trying to create a rule that allows access to whatever files are contained within but that will redirect any requests for the folder itself or non-existant entries to another location.

ie:

/subfolder
/subfolder/
/subfolder/random/
/subfolder/this_file_does_not_exist.jpg

> website.url

If file exists, allow linking.

Here is what I have at the moment...

RewriteCond %{REQUEST_URI} !^(.*)$
RewriteRule / http://url.com/ [R=301,L]


For the purposes of this test I have disabled the hotlinking code in the root.

I do have a general redirect in the root too.

Would it be easier/possible to place this code in there, or not?

Here is the applicable root code:

RewriteCond %{REQUEST_URI} !^/(subfolder)/ 
RewriteRule ^(.*)$ http://subdomain.website.url/$1 [R=301,L]


Where am I going wrong, please? =]

Thanks, Nic

jdMorgan

4:40 pm on Jun 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The main problem is that your code does not match your stated requirements.

If you want to redirect the "folder-only" URL, then you must detect requests for "/folder/ followed by nothing." You may wish to detect requests for "/folder followed by nothing" as well (no trailing slash) in order to avoid mod_dir doing a redirect to add that slash.
The regex pattern for that would be ^/folder/?$

If you want to redirect if the requested image does not exist, then you must check for file-exists. Otherwise, the usual 404-Not Found error handling will be invoked for missing images.

In your top-level .htaccess, this would give:

# Redirect if subfolder index is requested
RewriteRule ^subfolder/?$ http://subdomain.example.com/ [R=301,L]
#
# Redirect if non-existent file in subfolder is requested
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^subfolder/(.+)$ http://subdomain.example.com/$1 [R=301,L]

Note that we usually do not recommend redirecting requests for non-existent resources unless you have absolutely no concern for search engine ranking of those resources or that pages that link to them. If SE ranking is a concern, then using the server's normal 404 response to provide a page with a link to the desired 'destination' for users (both people and SE robots) making bad requests is to be strongly preferred.

Further, the more restrictive you can make the second rule above, the better. File- and directory-exists checks invoke the operating system's file-handler and may result in a read of the physical disk if the cached filesystem state is 'dirty' (not up to date). This can be thousands of times slower than executing a 'normal' mod_rewrite directive, and of course, it can also beat your hard drive to death. Therefore, enhancing the rule by using a more-specific rule pattern or by adding an exclusionary RewriteCond at the top to only check the disk for certain filetypes --or equivalently, to NOT check the disk for certain other filetypes-- is a good idea...

Note that RewriteConds are not evaluated unless the RewriteRule pattern matches (see the mod_rewrite documentation).

This code can be 'ported' back into /subfolder/.htacess if you like. Having two alternatives, I simply picked the one that resulted in a more-specific example.

Jim

Mikroz

5:57 pm on Jun 9, 2010 (gmt 0)

10+ Year Member



Hey Jim,

Thank you for your very informative reply. I learn something new every time. =]

Perhaps my reasoning for such a code might serve to resolve some of queries...

The folder will store a few images which I intend using in my email signature.

I cannot limit access to these files by IP because I do not have a static IP and because I access my webmail from various locations.

Thus I wanted to redirect anyone trying to access this folder or non-existant files within it.

Cheers, Nic

jdMorgan

6:05 pm on Jun 9, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Seems that a 403-Forbidden response would be simpler to implement, in that case.

Jim

Mikroz

6:25 pm on Jun 9, 2010 (gmt 0)

10+ Year Member



Hey Jim,

I'm not quite sure how to go about doing that but I'm happy to have random discoverers redirected to a useable resource anyways. :)

I thought I was getting a little more adept with this .htaccess stuff but seeing your code has made me realize that I need to keep reading. Hehe.

Have a good day/eve'.

Cheers and thanks, Nic