homepage Welcome to WebmasterWorld Guest from 54.198.94.76
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirect file request
when it may appear in more than one file path
cookie2




msg:4605466
 3:44 pm on Aug 28, 2013 (gmt 0)

I'm trying to redirect any requests for thisfile.htm. The problem I have is it may be requested from several different locations since it's been moved over the years and some search engines still look for the old locations, thus throwing error log errors.

What I have is:

RewriteRule ^thisfile.htm$ http://example.com/level1/thisfile.htm [R=301]

That works fine when the file is requested from root (http://example.com/thisfile.htm). But if one of the old paths is requested (http://example.com/level1/level2/level3/thisfile.htm) it doesn't work.

How can I make requests for this file redirect no matter what path is used in the request?

 

JD_Toims




msg:4605470
 3:52 pm on Aug 28, 2013 (gmt 0)

How can I make requests for this file redirect no matter what path is used in the request?

As long as the new file does not end with the same name you can simply remove the anchor from the beginning of the rule:

RewriteRule thisfile\.htm$ http://example.com/level1/thisfile.htm [R=301,L]

* Note the addition of the \ preceding the . [dot] which makes it match a literal . [dot] rather than any character other than the end of a line and the L flag which should always be used unless you know why you do not want it.

If the new file is the same name as the old file, then you'll need to remove the opening anchor and use a condition:

RewriteCond %{REQUEST_URI} !^/level1/thisfile\.htm$
RewriteRule thisfile\.htm$ http://example.com/level1/thisfile.htm [R=301,L]

* Note the ! at the beginning of the condition which means *not* whatever pattern follows, so the condition says: If the requested URI is not /level1/thisfile.htm then proceed with the redirect.

cookie2




msg:4605486
 5:23 pm on Aug 28, 2013 (gmt 0)

Thank you. Could you tell me why removing the ^ made a difference? I thought that indicated the start of the match pattern.

JD_Toims




msg:4605494
 5:40 pm on Aug 28, 2013 (gmt 0)

I thought that indicated the start of the match pattern.

It does, so ^thisfile\.htm$ will only match *exactly* http://www.example.com/thisfile.htm. By removing the start anchor from the rule *anything* preceding thisfile.htm is implicitly matched, so thisfile\.htm$ will match http://www.example.com/thisfile.htm and http://www.example.com/some-path/thisfile.htm and http://www.example.com/some-other/path/thisfile.htm and anything else that ends with thisfile.htm.

cookie2




msg:4605502
 6:10 pm on Aug 28, 2013 (gmt 0)

Gotcha! ::two thumbs up::

Thanks for the help.

lucy24




msg:4605540
 8:42 pm on Aug 28, 2013 (gmt 0)

I thought that indicated the start of the match pattern.

Not the match pattern: the test string. More exactly: the part of the test string that the RegEx engine (here a RewriteRule located within htaccess) can see.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved