.htaccess - 简化重定向域到非 www SSL

标签 .htaccess ssl redirect

我正在使用以下 .htaccess 代码来执行以下操作:

.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/

相关文章:

css - 是否可以使用htaccess压缩较少的CSS文件? (以及如何?)

apache - 将所有 https 请求重定向到 http

java - 这段 Java 1.7 代码片段中的 TLS 版本是什么?

php - 连接到 www.googleapis.com 时出现未知 SSL 协议(protocol)错误 :443 PHP google anaytlics api

apache - 使用 .htaccess 将子目录重定向到根 URL

JavaScript:发送 POST,重定向到响应

.htaccess - 重写 htaccess 以包含 www 并包含 url 的其余部分

php - 当文件(已删除扩展名)和文件夹同名时如何正确链接?

ssl - 加载扩展部分 usr_cert 时出错

将 CloudFlare 上的子域重定向到 Google 日历