【问题标题】:Redirecting www to non-www, http to https and domain.com to domain.com/index.php将 www 重定向到非 www,将 http 重定向到 https,将 domain.com 重定向到 domain.com/index.php
【发布时间】:2017-10-03 12:51:27
【问题描述】:

我正在尝试使用 .htaccess 进行重定向:

1) 所有 www 到非 www 例如www.example.com/page.php 到 example.com/page.php

2) 所有 http 到 https 例如http://example.com/page.phphttps://example.com/page.php

3) 域(和子文件夹)到它们的 index.php 页面,例如https://example.comhttps://example.com/index.phphttps://example.com/en-gb/https://example.com/en-gb/index.php

我可以让 1) 和 2) 正常工作:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI} 

但我也在努力包含 3)。我已经尝试过了,但没有运气:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php

更新 - 试过了,但没有成功

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}

Redirect / https://example.com/index.php

第二次更新 - 尝试了这个,但与我将 index.php 递归添加到 URL 之前相同:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
RewriteRule ^ https://%2%{REQUEST_URI}

RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) https://example.com/index.php$1

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    清除浏览器缓存并将以下代码放在主目录.htaccess文件中,无需去每个文件夹都放代码:

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*) https://yoursite.com/$1 [L,R=302]
    # the two lines above will catch both http://www & http:// and force https:// 
    RewriteCond %{HTTPS} =on
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule ^(.*) https://yoursite.com/$1 [L,R=302]
    # the three lines above will catch only https://www and redirect it to none wwww 
    RewriteCond %{REQUEST_URI}  ^/$ [NC]
    RewriteRule  ^    /index.php [R=302,L,NE]
    # the two lines above will force index.php to domain when request to index.php comes without it
    RewriteCond %{REQUEST_FILENAME} -d  
    RewriteRule  ^(.*)/$    /$1/index.php [R=302,L,NE]
    # the two lines above will force index.php to any directory when request to index.php comes without it
    

    测试一下,如果没问题,把每个302改成301,得到永久重定向

    【讨论】:

    • 比我自己的答案更好,因为所有操作都在一个 .htaccess 文件中完成 - 谢谢!
    【解决方案2】:

    嗯-谢谢大家的帮助:)

    我最终到达了那里。万一这可以帮助其他有同样问题的人......这进入了根目录:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
    RewriteCond %{HTTPS} !on
    RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
    RewriteRule ^ https://%2%{REQUEST_URI}
    
    RewriteCond %{HTTP_HOST} ^example\.com$
    RewriteRule ^$ https://example.com/index.php
    

    每个文件夹中都有这个:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www\.)(.+) [OR]
    RewriteCond %{HTTPS} !on
    RewriteCond %{HTTP_HOST} ^(www\.)?(.+)
    RewriteRule ^ https://%2%{REQUEST_URI}
    
    RewriteCond %{REQUESTFILENAME} !-f
    RewriteRule ^$ https://example.com/en-gb/index.php
    

    【讨论】: