我正在使用以下 .htaccess 代码来执行以下操作:
- 将 example.com 重定向到 https://example.com
- 将 wwww.example.com 重定向到 https://example.com
- 将
/$1
之后的所有内容重写为index.php?get=$1
.htaccess
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(?:www.)?(.*)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteRule ^([^.]+)/?$ index.php?get=$1 [L]
这工作正常,但这段代码还能简化吗?
最佳答案
您可以将 http->https
规则和 www
删除规则合并为一个规则,并让您的 .htaccess 如下所示:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ index.php?get=$0 [L,QSA]
关于.htaccess - 简化重定向域到非 www SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44330473/