Forum Moderators: phranque

Message Too Old, No Replies

.htaccess 301 redirect & regular expressions?

         

collings28

1:26 pm on May 7, 2009 (gmt 0)

10+ Year Member



Hi,

I'm having issues with a .htaccess 301 redirect.

I would like to redirect:

http://www.exmaple.com/hei-by-alfred-sung-for-men-100ml-edt-spray.html

to

http://www.example.com/mens-aftershave/alfred-sung/hei-100ml-edt-spray.html

The code being used is:

Redirect 301 /([\w-]+)-by-([\w-]+)-for-men-([\w-]+)\.html http://www.example.com/mens-aftershave/$2/$1-$3.html

I need any page that shows '-for-men-' in the old url to redirect to '/mens-aftershave/' then show the brand in folder format (alfred-sung/), then the product name (hei) then the remaining details (100ml-edt-spray).

Currently, the 301 is being ignored (or redirecting to the wrong page) and I'm being redirected a 404 error page.

Thanks in advance for any help.

[edited by: jdMorgan at 3:12 pm (utc) on May 7, 2009]
[edit reason] example.com [/edit]

jdMorgan

3:13 pm on May 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This may be caused by your server version, and the limitations of the regular-expressions library used by that version. Apache 1.3x uses POSIX regular expressions and uses the library bundled with the operating system. Apache 2.x includes support for PCRE. So, the PERL-Compatible Regular Expressions such as "\w" may not work on your server.

If that is the case, replace each instance of "[\w-]+" with "[_0-9A-Za-z\-]+".

Refer to the Apache mod_rewrite documentation for your server version, and use only the regular-expressions tokens that are documented as supported on your server.

Also, for the sake of efficiency, add an [L] flag to each of your rules, unless you know why you don't want to use it.

Jim