.htaccess - 删除www,强制https,删除php并删除尾部斜杠
I am trying to archieve the following with least amount of redirects;
- Remove WWW
- Force HTTPS
- Remove php extension
- Remove end trailing slash
What i have so far and is working:
RewriteEngine On
# REMOVE WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# FORCE HTTPS
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# REMOVE TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# REMOVE PHP EXTENSION
RewriteRule ^(.+)\.php$ /$1 [NC,L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [END]
Current behaviour:
http://www.example.com/functions.php -> https://example.com/functions
(works with 4 redirects)
OR
http://www.example.com/functions/ -> https://example.com/functions
(works with 4 redirects)
Does anyone have any suggestions how to make this work with fewest possible redirects?
我试图用最少量的重定向来实现以下目的; p>
- 删除WWW li>
- 强制HTTPS li>
- 删除php扩展 li>
- 删除尾部斜杠 li>
我到目前为止所做的工作: p>
RewriteEngine On #REMOVE WWW RewriteCond%{HTTP_HOST} ^ www \。(。*)$ [NC] RewriteRule ^(。*)$ http://%1 / $ 1 [R = 301,L] #FORCE HTTPS RewriteCond%{HTTP:X-Forwarded- Proto}!https RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R = 301] #REMOVE TRAILING SLASH RewriteCond%{REQUEST_FILENAME}!-d RewriteRule ^(。*) / $ / $ 1 [L,R = 301] #REMOVE PHP EXTENSION RewriteRule ^(。+)\ .php $ / $ 1 [NC,L,R = 301] RewriteCond%{REQUEST_FILENAME} .php - f RewriteRule ^(。+)/?$ / $ 1.php [END] code> pre>
当前行为: p>
http://www.example.com/functions.php - > https://example.com/functions
(有4次重定向的作品) p>
或 p>
http://www.example.com/functions/ - > https://example.com/functions
(有4次重定向的作品) p>
有没有人有任何建议如何 使用尽可能少的重定向来完成这项工作? p> div>
It doesn't hurt to just always rewrite to https and lose the www in one step. Trailing slash is unchanged, but lost a line on removing php-extenstion by inverting the Cond.
# REMOVE WWW & FORCE HTTPS
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
# REMOVE TRAILING SLASH
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# REMOVE PHP EXTENSION if there's no file with this name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.php$ /$1 [NC,L,R=301]
If you also need handling for files that were requested as file/
without .php
, you should stay with your code for the final part:
RewriteRule ^(.+)\.php$ /$1 [NC,L,R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [END]