rishijd
rishijd

Reputation: 1344

htaccess mod rewrite with optional slash

We're switching from an old site to a new site with better URLs for SEO. I'm trying to do this:

RewriteRule ^products/boots/materialid/(.*)/colour/(.*)$ http://www.mydomain.com/boots/$2/$1 [R=301,L]

However the problem is:

1) Instead of using (.*), can I specify that this can have the following characters

2) If the above won't work, and for my general knowledge on this, how do I update the above so that a trailing slash is optional? E.g. for the above rule, going to

www.mydomain.com/materialid/leather/colour/blue/

sends me to

www.mydomain.com/blue//leather

The extra slash comes because of the slash after "blue" in the original URL, but I need to exclude this.

Upvotes: 0

Views: 4648

Answers (1)

daiscog
daiscog

Reputation: 12078

Try this:

RewriteRule ^products/boots/materialid/([^/]*)/colour/([^/]*)/?$ http://www.mydomain.com/boots/$2/$1 [R=301,L]

[^/] matches any character that is NOT a slash. /? means an optional trailing slash (note that it is outside of the capturing parentheses, so that it will not be included in the rewritten URL).

EDIT

As per your comment, to add an optional /index.php:

RewriteRule ^products/boots/materialid/([^/]*)/colour/([^/]*)(/|/index\.php)?$ http://www.mydomain.com/boots/$2/$1 [R=301,L]

Upvotes: 3

Related Questions