Combining rewrites for non-www/ssl/trailing slash with upper->lowercase in .htaccess

Viewed 144

So I have a straightforward rewrite to catch non-www URLs, non-SSL urls and urls missing a trailing slash to redirect to SSL, www and trailing slash using:

<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$  [NC]
RewriteCond %{HTTP_HOST} ^(.*[^/])$ [NC]
RewriteRule (.*\/)$ https://www.tierpoint/$1 [R=301,L] 
RewriteRule (.*[^/])$ https://www.tierpoint/$1/ [R=301,L]
</IfModule>

I'd like to add a rewrite to drive uppercase requests to lowercase (sparing anything in a query string)

I know I can loop througRewriteRule [A-Z] - [E=HASCAPS:TRUE,S=1]

RewriteRule ![A-Z] - [S=28]
RewriteRule ^([^A]*)A(.*)$ $1a$2
...RewriteRule ^([^Z]*)Z(.*)$ $1z$2
RewriteRule [A-Z] - [N]
RewriteCond %{ENV:HASCAPS} TRUE
RewriteRule ^/?(.*) /$1 [R=301,L]

But how can I combine these?

1 Answers

Have it like this in your site root .htaccess:

RewriteEngine On

# add www, https
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI}  [R=301,L,NE]

# add a trailing slash is query string is not present
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]+?[^/]$ %{REQUEST_URI}/ [L,R=301,NE]

# convert URI to lowercase
# This requires Apache 2.4
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond expr "tolower(%{REQUEST_URI}) =~ m#(.+)#"
RewriteRule [A-Z] %1 [L,NE,R=301]
Related