.htaccess,强制HTTPS并删除index.php

时间:2017-01-25 16:10:44

标签: apache .htaccess redirect mod-rewrite

我知道有多个帖子,但我无法用其他答案解决。

我确实尝试过:htaccess force www and remove index.php.htaccess force HTTPS但没有成功。

我的网站有一个执行路由器的index.php。所以链接" / about"实际上是从index.php管理的(要在链接中删除)。

这是我目前的htaccess:

<IfModule mod_rewrite.c>

    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
    RewriteCond %{HTTP_HOST} !=localhost [NC]
    RewriteCond %{HTTP_HOST} !=127.0.0.1
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteCond %{THE_REQUEST} ^.*/index.php 
    RewriteRule ^(.*)index.php$ %{HTTP_HOST}/$1/ [R=301,L]

</IfModule>

它对index.php非常有效(实际上它会删除index.php并重写URL)。

它可以从example.com重写为https://www.example.com,这没关系,我想要这个!)。但是没有 http://www.example.com 重写为 https://www.example.com

例如,链接为&#34; http://www.example.com/this/is/a/not/link/secure&#34;保持原样。 但我希望重定向到&#34; https://www.example.com/this/is/a/not/link/secure&#34;

非常感谢。

编辑:根据anubhava的回答,这是新的整个htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L,QSA]

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!localhost$|127\.0\.0\.1$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L,NC,NE]

现在用双域重写http://www.example.com/apiv2/member-card?id=12中的https://www.example.com/www.example.com//?id=12 ..... 与在http://example.com/apiv2/member-card?id=12

中重写的https://www.example.com/www.example.com//?id=12相同

1 个答案:

答案 0 :(得分:0)

您的第一条规则需要[OR]子句来重定向这两个条件:

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!localhost$|127\.0\.0\.1$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L,NC,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

确保在测试此更改之前清除浏览器缓存。

相关问题