Welcome to WebmasterWorld Guest from 54.147.44.93

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Redirect file request

when it may appear in more than one file path

     

cookie2

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

10+ Year Member



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

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

WebmasterWorld Senior Member Top Contributors Of The Month



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

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

10+ Year Member



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

JD_Toims

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

WebmasterWorld Senior Member Top Contributors Of The Month



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

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

10+ Year Member



Gotcha! ::two thumbs up::

Thanks for the help.

lucy24

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

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



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.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month