问题
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:
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]
来源:https://stackoverflow.com/questions/65101678/combining-rewrites-for-non-www-ssl-trailing-slash-with-upper-lowercase-in-htac