Issue
At the moment I'm using an underscore the hyphen 301 on my site, this is working well but I also want to convert all links to lowercase and add an uppercase to lower case rewrite.
I can do this but with the current method it would do the following which I don't think would be very good for SEO:
- Original URL
- 301
- Hyphen Replaced
- 301
- Hyphen Lowercase URL
How can I combine the following rewrites into one query so that urls with either underscores or uppercase are converted to hyphens and lowercase via a 301?
Underscore to Hyphens rewrite
RewriteRule ^post/([^_]*)_([^_]*_.*)$ /posts/new-category-1/$1-$2 [L,NE]
RewriteRule ^post/([^_]*)_([^_]*)$ /posts/new-category-1/$1-$2 [L,NE,R=301]
RewriteRule ^forum/([^_]*)_([^_]*_.*)$ /forums/new-category-1/$1-$2 [L,NE]
RewriteRule ^forum/([^_]*)_([^_]*)$ /forums/new-category-1/$1-$2 [L,NE,R=301]
Uppercase to Lowercase rewrite
httpd.conf
RewriteMap lc int:tolower
.htaccess
RewriteCond %{REQUEST_URI} ^[^A-Z]*[A-Z].*
RewriteRule ^ ${lc:%{REQUEST_URI}} [L,R=301]
Solution
You can avoid multiple 301
by having rules like this:
RewriteRule ^(post|forum)/([^_]*)_([^_]*_.*)$ /$1/$2-$3 [L,NE]
# if there is any upper case letter then do both lowercase conversion AND
# underscore to hyphen replacement
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(post|forum)/([^_]*)_([^_]*)$ /${lc:$1}/${lc:$2}-${lc:$3} [L,NE,R=301]
# otherwise regular underscore to hyphen replacement
RewriteRule ^(post|forum)/([^_]*)_([^_]*)$ /$1/$2-$3 [L,NE,R=301]
# and regular lower case conversion
RewriteRule ^(post|forum)/[^A-Z]*[A-Z] ${lc:%{REQUEST_URI}} [L,R=301]
Answered By - anubhava