【问题标题】:How to remove index.php from htaccess when redirect 301 - Codeigniter重定向 301 时如何从 htaccess 中删除 index.php - Codeigniter
【发布时间】:2016-07-02 21:49:17
【问题描述】:

当 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/$1 [L]

#non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [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/$1 [R=301,L]

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

提前致谢。

【问题讨论】:

    标签: apache .htaccess codeigniter redirect codeigniter-3


    【解决方案1】:

    尝试切换规则的顺序。

    RewriteEngine on
    
    #non-www to www
    RewriteCond %{HTTP_HOST} !^www\.(.*)
    RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [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/$1 [R=301,L]
    
    # Redirect to index.php internally
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
    

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

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

    【讨论】:

    • 非常感谢!!天才!!
    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    • 2012-04-03
    • 2019-11-23
    相关资源
    最近更新 更多