【问题标题】:htaccess by adding index.php in the root of the sitehtaccess 通过在站点的根目录中添加 index.php
【发布时间】:2016-12-21 19:07:57
【问题描述】:

这是我当前的 htaccess:

RewriteEngine on

RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_URI} !^/1EE772B8EBDDECBA378B575830E7E1B3\.txt$
RewriteCond $1 !^(index\.php|assets|wmt|google1f91ad48697ced36\.html|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

RewriteCond %{HTTP_HOST} ^domain.com.br$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
Options -Indexes

在第一个块行中,它将 http 重定向到 https; 在第二个块中,它允许直接访问并删除 index.php

如果 www 不存在,则在第三个块中添加 www。

最终目标是拥有如下网址:

https://www.domain.com.brhttps://www.domain.com.br/foo/bar

问题是当用户输入地址时 Domain.com.br(没有 www 和 http,仅在站点的根目录中)它重定向到:

https://www.domain.com.br/index.php

只有在站点的根目录才会添加这个 index.php

有人知道我哪里出错了吗?

我也接受 htaccess 优化建议

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    您可以使用这些重构规则:

    Options -Indexes
    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
    RewriteCond %{HTTP:X-Forwarded-Proto} =http
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
    
    RewriteCond %{REQUEST_URI} !^/1EE772B8EBDDECBA378B575830E7E1B3\.txt$
    RewriteCond $0 !^(index\.php|assets|wmt|google1f91ad48697ced36\.html|robots\.txt) [NC]
    RewriteRule .* index.php/$0 [L]
    

    完全清除浏览器缓存后进行测试。

    【讨论】:

    • 感谢您的帮助。我测试了你的建议,apache 返回 500。出了点问题:/
    • 啊!我有一个错字。现在已修复。你能重试吗?
    • 完美!你是个天才:)
    猜你喜欢
    • 1970-01-01
    • 2016-02-04
    • 2017-01-12
    • 2013-03-06
    • 2012-04-27
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2013-12-31
    相关资源
    最近更新 更多