Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect uppercase URL's to lowercase except *** - htaccess

I'm trying to redirect uppercase URL's to lowercase, but having a bit of a nightmare with it! (Mainly because my .htaccess knowledge is lacking!)

Currently I have:

<IfModule mod_speling.c>
CheckSpelling on
</IfModule>

RewriteEngine On
RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]

Which works fine, but the CMS I'm using puts pagination links in the URL such as http://website.com/blog/P8 or http://website.com/blog/P10 and because the URL's have an uppercase P (Which seems to be required) they are 404 or 301 redirecting.

Is there a rule i could add to make it not pick up on segments of the URL that have a P and immediately have at least one numerical character after it? Regex maybe?

Any help would be appreciated!

like image 555
Jason Mayo Avatar asked Dec 07 '25 07:12

Jason Mayo


1 Answers

You can create an exception like this:

RewriteEngine On

RewriteMap lc int:tolower

RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_URI} !/P\d+/?$ [NC] 
RewriteRule (.*) ${lc:$1} [R=301,L]

Or using negative lookahead:

RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^(?!.*/P\d+/?$)(.*)$ ${lc:$1} [R=301,L]
like image 199
anubhava Avatar answered Dec 09 '25 20:12

anubhava