重定向和重写URL并用Dash替换空间

时间:2014-04-11 16:07:15

标签: .htaccess mod-rewrite redirect url-rewriting clean-urls

我想重写/重定向我网站上的网址。以下是我想要完成的事情:

  1. 从www.olddomain.com重定向到www.newdomain.com
  2. 重写网址并创建干净的网址
  3. 创建干净的网址时忽略某些参数
  4. 用短划线替换空格%20。
  5. 以下是我网站上的一些示例网址以及我想要重写/重定向到的内容:

    www.olddomain.com/events/events.php?location=los%angeles www.newdomain.com/events/los-angeles

    www.olddomain.com/nightclubs/nightclubs.php?location=los%angeles www.newdomain.com/nightclubs/los-angeles

    www.olddomain.com/nightclubs/nightclubDetail.php?venueID=1234&userID=1346 www.newdomain.com/venue/1234

    www.olddomain.com/nightclubs/nightclubs.php?cat_type=bars&location=los%angeles www.newdomain.com/nightclubs/bars/los-angeles

    www.olddomain.com/events/events.php?location=los%angeles www.newdomain.com/events/los-angeles

    www.olddomain.com/events/eventsDetail.php?eventID=1234&slid=5678&lid=5432 www.newdomain.com/events/1234

    此外,已在Google中编入索引的网址包含位置字符串,例如:?location = los%20angeles

    这就是我的XML站点地图中的URL的编写方式。

    如何让Google将当前已编入索引的网址替换为位置字符串为的新网址:?location = los-angeles

1 个答案:

答案 0 :(得分:1)

将这些添加到olddomain.com的htaccess应该有效:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\?cat_type=(.*)&location=(.*)%(.*)\ HTTP
RewriteRule ^ http://newdomain.com/%3/%4/%5-%6? [R=301,L]

RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\?venueID=(.*)&userID=(.*)\ HTTP
RewriteRule ^ http://newdomain.com/venue/%4? [R=301,L]

RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\?location=(.*)%(.*)\ HTTP
RewriteRule ^ http://newdomain.com/%3/%4-%5? [R=301,L]

RewriteCond %{THE_REQUEST} ^(GET|POST)\ /(.*)/(.*)\.php\?eventID=(.*)&slid=(.*)&lid=(.*)\ HTTP
RewriteRule ^ http://newdomain.com/%2/%4? [R=301,L]