【发布时间】:2016-02-10 23:12:31
【问题描述】:
使用 .htaccess 我最近使用以下代码将我所有的网站 http:// 请求重定向到 https://:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
301 重定向正在运行,现在每个人都登陆了 https。但是,当用户使用带有书签的链接 http:// 或者我向他们发送格式为 http:// 而不是 https:// 的 URL 时,他们会访问与单击链接时不同的 URL在网站上。
如果是来自主页的链接,则 URL 如下所示:https://www.example.com/rest_of_url
如果使用 http (http://www.example.com/rest_of_url) 将链接发送给客户,并且他们点击链接将加载:https://www.example.com/index.php?/rest_of_url
我尝试使用 .htaccess 和以下代码从 URL 字符串中删除 index.php?/ 但这没有效果:
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php\?/ [NC]
RewriteRule (.*?)index\.php\?/ / [R=301,NE,L]
有什么建议可以阻止人们使用 index.php 登陆 URL?/ 会很棒
根据要求,这是整个 htaccess 文件: 重写引擎开启
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET.*index\.php\?/ [NC]
RewriteRule (.*?)index\.php\?/ / [R=301,NE,L]
【问题讨论】:
-
在问题上方添加
-
重写规则 ^(.*)$ /index.php?/$1 [L]
-
第二种情况的 RewriteCond %{HTTP_HOST} !^www\。不需要,您已经发送了所有非 www。对 https://www 的请求。这只是一个评论,而不是一个答案。 L 表示最后一个,所以第一个 RewriteCond %{HTTP_HOST} !^www\。已经将用户发送到 https 和 www,因此您唯一需要做的就是确保将 http://www.yoursite.com 发送到 https://www.yoursite.com
标签: apache .htaccess url mod-rewrite redirect