【问题标题】:Redirect 301 with params使用参数重定向 301
【发布时间】:2015-07-08 17:31:52
【问题描述】:
我正在重定向旧网站和新网站之间的流量,我需要将 301 从 /hotel.php?hotel=lasvegas&lng=es 重定向到 http://www.new-web.com/hotel-las-vegas/
htaccess 怎么可能?
我试过了
Rewriterule ^hotel\.php\?hotel=lasvegas\&lng=es http://www.new-web.com/hotel-las-vegas/ [L,R=301]
我认为我的错误在于正则表达式。谁能帮帮我?
【问题讨论】:
标签:
regex
apache
.htaccess
mod-rewrite
redirect
【解决方案1】:
您可以在 DOCUMENT_ROOT/.htaccess 文件中使用此代码:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /hotel\.php\?hotel=lasvegas&lng=es [NC]
RewriteRule ^ hotel-las-begas? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^hotel-las-vegas/?$ hotel.php?hotel=lasvegas&lng=es [L,QSA,NC]
【解决方案2】:
在 htaccess 的最佳位置插入重定向 301 的最佳代码非常重要。例如 :
http://www.abartazeha.com/
我们替换 abartazeha.com 而不是 abartazeha.ir
...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]