Welcome to WebmasterWorld Guest from 54.161.255.61

Forum Moderators: Ocean10000 & incrediBILL & phranque

Question about 301 redirect using a wildcard

   
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
4:54 am on Feb 10, 2014 (gmt 0)

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



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.
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?
9:20 pm on Feb 10, 2014 (gmt 0)

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



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).
 

Featured Threads

Hot Threads This Week

Hot Threads This Month