【问题标题】:Index.php ? in url after 301 redirect索引.php?在 301 重定向后的 url
【发布时间】:2017-12-30 21:23:55
【问题描述】:

我已将所有内容从旧站点移动到新站点,现在我想将所有页面从旧站点 301 重定向到具有相同 URL 的新站点。

例子:

  • http://www.oldsite.net/en/newspage.htmlhttp://www.newsite.com/en/newspage.html

  • http://www.oldsite.net/en/aboutus.htmlhttp://www.newsite.com/en/about.html

这是我的.htaccess:

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]


RewriteCond %{HTTP_HOST} ^www\.olddoamin\.net$ [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L]

这会重定向新网站同一页面上的所有页面,

但新站点在重定向后的 URL 中有 index.php?。例如:

http://www.newsite.com/index.php?/en/about.html

网站是用 codeigniter 开发的。

有人知道为什么会这样吗?

【问题讨论】:

  • 您的$config['index_page'] 值是多少?
  • $config['index_page'] = ' ';
  • 只需将.htaccess 的最后两行移至RewriteEngine On 行的顶部或下方即可。
  • 谢谢anubhava,但它不起作用:(

标签: .htaccess codeigniter


【解决方案1】:

正如 cmets 中提到的 @anubhava,您的指令顺序错误。您的重定向需要前端控制器之前进行。 (作为一般规则,外部重定向应该总是在内部重写之前。)例如:

RewriteCond %{HTTP_HOST} ^www\.olddoamin\.net$ [NC]
RewriteRule ^(.*)$ https://www.newdomain.com/$1 [R=301,L]

RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

但是,您需要在测试前清除浏览器缓存,因为较早的(错误的)301 重定向将被浏览器硬缓存。

如果您在前端控制器之后有重定向,那么请求首先被重写为/index.php?/en/about.html,然后被重定向。因此,为什么重定向会搞砸。

【讨论】:

    猜你喜欢
    • 2012-01-27
    • 2011-09-01
    • 2014-10-26
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    相关资源
    最近更新 更多