homepage Welcome to WebmasterWorld Guest from 54.227.41.242
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

    
Question about 301 redirect using a wildcard
colbyp




msg:4643807
 4:28 am on Feb 10, 2014 (gmt 0)

I have a series of dynamic URLs from the old site that need to be redirected to the static URL on the new site. I think I need to use a wildcard in the rewrite rule but not sure.

Example -- multiple versions of the same URL:

www.example.com/page-category/page/20130908f521
www.example.com/page-category/page/20131026a640
www.example.com/page-category/page/20131025y307
etc.

I want to redirect them all to:

www.example.com/page-category/page/

Sooo, something like this?:

RewriteRule^page/(.*)$http://www.example.com/page-category/page/[R=301,L]

Thanks all for help

 

lucy24




msg:4643816
 4:54 am on Feb 10, 2014 (gmt 0)

I hope that was an artifact of cutting-and-pasting. I think your server will explode if you try to feed it a RewriteRule with no spaces.

:: detour to test site ::

Yup. (This is a relief. Last time I checked something, I turned out to be 100% mistaken.)

Once you're in mod_rewrite, all you have to do is leave off the closing anchor:

^page-category/page/

will match anything that begins that way. You don't need the (.*), because you're not capturing. Keep the opening anchor, as it will shave a nanosecond or two off processing.

colbyp




msg:4643866
 12:54 pm on Feb 10, 2014 (gmt 0)

Thank you. And a clarification question about redirect loops:

If my rule is:

RewriteRule ^page-category/page/ http://example.com/page-category/page/ [R=301]

and a user types in the new preferred URL http://example.com/page-category/page/, will that keep redirecting to itself?

lucy24




msg:4643984
 9:20 pm on Feb 10, 2014 (gmt 0)

Oops, yes, my bad. If the target and the pattern are the same, you will always get an infinite loop. So you'll need to add one character to the rule and make the pattern

^page-category/page/.

. here means "anything at all". That way your target and pattern are different and there's no loop.

Edit: Also make sure the rule includes the [L] flag. It isn't implied by [R=301] the way it would be for [G] or [F] (or, I think, any non-300 code).

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