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

蓝咒 提交于 2021-02-07 09:05:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!