Forum Moderators: phranque

Message Too Old, No Replies

Redirect files but not directory

Is it possible to redirect only files?

         

mahidhar

4:43 am on Mar 23, 2010 (gmt 0)

10+ Year Member



I would like to redirect files only but not directory using .htaccess.

Is it possible? Your help is appreciated.

tangor

5:14 am on Mar 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Short answer is YES. Have you looked at the forum library? Many good exampled.

Show us your best effort code. We won't write it for you, but will help you figure it out.

mahidhar

5:29 am on Mar 23, 2010 (gmt 0)

10+ Year Member



Sorry, If my post gave an impression like I did not put the effort.

URL rewriting is new to me. I tried to find an answer, I found other examples but not this one. I will go through the library posts and try to figure it out.

jdMorgan

12:22 pm on Mar 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If nothing else, you can pick up the "vocabulary" and see examples of the information required to answer this type of question. Looking at your best-effort code also gives us a good indication of any misunderstandings you may have, and often gives good clues as to the 'URL-scope' to which you wish your rule(s) to apply. It often saves a lot of time by showing where the 'starting point' for discussion should be.

Jim

mahidhar

5:34 pm on Mar 23, 2010 (gmt 0)

10+ Year Member



OK. I have gone through the tutorials in the library. Great stuff there. Below is the code that I think that will work for my requirment (To redirect all the URLs under old directory to URLS under new directory, but do not redirect URL for old drectory)

RewriteEngine on
RewriteRule ^/old_directory/(.+)$ /new_directory/$1 [R=301,L]

I will test this after going home tonight.

jdMorgan

5:55 pm on Mar 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the code is for use in .htaccess, the URL-path 'seen' by RewriteRule will not start with a slash. Therefore, the RewriteRule pattern should not start with a slash, or it will never match.

Jim

mahidhar

6:02 pm on Mar 23, 2010 (gmt 0)

10+ Year Member



Thanks Jim. Yes it is for .htaccess, got it the code without /

RewriteRule ^old_directory/(.+)$ /new_directory/$1 [R=301,L]

Is that it? Anyway, I will find it when I test this tonight.

I like the approach here, don't just feed the fish to the man rather teach him catching fish on his own.

g1smd

9:30 pm on Mar 23, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Redirects should have the protocol and domain name as a part of the target URL.

mahidhar

12:07 am on Mar 24, 2010 (gmt 0)

10+ Year Member



Thanks g1smd, I have put the protocol and domain name, I am still having issues

RewriteEngine On
RewriteRule ^1/old-virtual-dir/(.+)$ http://www.example.com/1/new-virtual-dir/$1 [R=301,L)

1 is a physical directory where as old-virtual-dir and new-virtual-dir as just URL-paths, they are not physical directories.

I want to be able to redirect

http://www.example.com/1/old-virtial-dir/vitual-file.html to http://www.example.com/1/new-virtual-dir/virtual-file.html

The above code is not working, It seems it is not executing at all nothing happens.

jdMorgan

12:57 pm on Mar 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code is correct, but two critical questions:

1) Where is this code located? It must be located in /example.com/.htaccess in order to work as written.
2) Do you have any other working RewriteRules? If not, you may need to add
 Options +FollowSymLinks -Indexes -MultiViews 

above your posted code to enable mod_rewrite.

Because URLs and filepaths/directories are entirely-different things, mod_rewrite does not 'care' if "directories" in URLs exist or not, so that is not a concern. URLs *always* "exist" -- from the moment they are published on a Web page. Whether they resolve to an existing directory or file (or to a script which produces content) does not matter as far as mod_rewrite is concerned. By this I mean that the function is not affected -- The code itself can indeed check file- and directory-exists, if so desired, after the URL is resolved to a filepath.

Jim