【问题标题】:htaccess doesnt remove index.php if WWW is in the URL如果 WWW 在 URL 中,htaccess 不会删除 index.php
【发布时间】:2017-09-14 13:04:32
【问题描述】:

使用 Htaccess,我可以从 url 中删除 www,但我一直在努力彻底删除 index.php。

我正在使用 codeigniter 框架并检查了我的配置文件。

$config['index_page'] = '';
$config['base_url'] = '';

我也试过了:正如 S.O 其他地方有人建议的那样:

$config['base_url'] = '/';

我的 htaccess 文件如下所示:
重写引擎开启 RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 [L]

#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [L,R=301]

我尝试了编写模式重写规则的所有不同版本,这个效果最好,但它在 URL 中添加了一个问号和一个正斜杠 重写引擎开启

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php?/$1 [L,QSA]

现在的结果,如果我输入www.mywebsite.com,它会变成mywebsite.com,这是正确的。

但是如果我输入 www.mywebsite.com/someuri 然后它变成 www.mywebsite.com/index.php?someuri

使用最后一个 modrewrite 规则,url 变为 www.mywebsite.com/?/someuri

然后我尝试在 index.php 之后添加问号,如下所示:

# Now the CodeIgniter part
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

对于后者,如果我在 URL 中输入 index.php,它不会消失。

我显然不懂 mod-rewrite,有人可以指导我正确的方向。

【问题讨论】:

  • 可能是因为您的强制非 www 规则中有 [L,R=301]L 表示最后一条规则,所以不要进一步处理。尝试从那里删除它。我目前无法对其进行测试以获得答案,我不在安装了 Apache 的机器上。
  • 你可能会发现这个 htaccess 测试器真的很方便http://htaccess.madewithlove.be/
  • 我去掉了[L] 结果还是一样。不过值得一试
  • 不要将基本网址留空

标签: .htaccess codeigniter indexing


【解决方案1】:

试试下面的

打开 config.php 并进行以下替换

$config['index_page'] = "index.php"

$config['index_page'] = ""

在某些情况下,uri_protocol 的默认设置无法正常工作。直接替换

$config['uri_protocol'] ="AUTO"

通过

$config['uri_protocol'] = "REQUEST_URI"

HTACCESS

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

【讨论】:

  • 还有$config['base_url'] 必须设置。例如$config['base_url'] = 'http://mywebsite.com/';(包括斜杠)
  • 谢谢,您建议的解决方案是我之前尝试过的解决方案。有用。但如果我添加。 www 到 url 然后 index.php?过来。此外,如果我在 URL 中手动插入 index.php,那么它仍然存在,不会被删除。
  • Codeigniter 3 版本没有 AUTO for uri_protocol
【解决方案2】:

试试下面的 打开 config.php 并执行以下替换

$config['index_page'] = '';

$config['index_page'] = 'index.php';

配置base_url

$config['base_url'] = '';

$ark_root           = "http://".$_SERVER['HTTP_HOST'];
$ark_root          .= str_replace(basename($_SERVER['SCRIPT_NAME']),"", $_SERVER['SCRIPT_NAME']);
$config['base_url'] = $ark_root;

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?/$0 [PT,L]

【讨论】:

    猜你喜欢
    • 2012-08-15
    • 2015-09-09
    • 1970-01-01
    • 2014-08-24
    • 2011-05-20
    • 2017-03-15
    • 1970-01-01
    • 2014-06-19
    相关资源
    最近更新 更多