我已将所有内容从旧网站移至新网站,现在我想将所有网页从旧网站重定向到新网站,并使用相同的网址。
示例:
http://www.oldsite.net/en/newspage.html
至http://www.newsite.com/en/newspage.html
http://www.oldsite.net/en/aboutus.html
至http://www.newsite.com/en/about.html
Thsi是我的.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]
这会重定向新网站上同一页面上的所有网页
但重定向后,新网站的网址中包含index.php?
。例如:
http://www.newsite.com/index.php?/en/about.html
网站是在codeigniter中开发的。
任何人都知道为什么会这样?
答案 0 :(得分:1)
正如@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
,然后重定向。因此,为什么重定向会搞砸。