如何在重定向 301 时从 htaccess 中删除 index.php - Codeigniter

How to remove index.php from htaccess when redirect 301 - Codeigniter

当 url 类型为:

时,我在 Codeigniter 中遇到问题
example.gov.ar/news (without www and .gov)

我需要它重定向到:

www.example.gob.ar/news (with www and .gob)

但它重定向

www.example.gob.ar/index.php/news

这是我的 htaccess 文件:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]

#non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/ [R=301,L]

#.gov.ar to .gob.ar
RewriteCond %{http_host} ^example.gov.ar$ [OR]
RewriteCond %{http_host} ^www.example.gov.ar$ 
RewriteRule ^(.*)$ http://www.example.gob.ar/ [R=301,L]

注意:我的配置文件有 $config['index_page'] = '';

提前致谢。

尝试切换规则的顺序。

RewriteEngine on

#non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/ [R=301,L]

#.gov.ar to .gob.ar
RewriteCond %{http_host} ^example.gov.ar$ [OR]
RewriteCond %{http_host} ^www.example.gov.ar$ 
RewriteRule ^(.*)$ http://www.example.gob.ar/ [R=301,L]

# Redirect to index.php internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/ [L]

根据所有规则重新评估由 .htaccess 触发的外部重定向,因此顺序很重要。

在更改后的顺序中,外部重定向首先发生,因此只有最后的 URL 与第三个 RewriteRule 匹配,然后它就不会在外部可见。