下面是在博客上看我的旧post地址的方法:
subdomain.domain.com/view/1310/article-title/
当访问者来自Google这样的地址时,我希望它被redirect,如下所示:
http://www.domain.com/article-title/
我需要指定一些关于旧的/第一个链接的细节:
我试过这个:
Options +FollowSymLinks RewriteEngine On RewriteCond %{HTTP_HOST} subdomain.domain.com $ [NC] RewriteRule ^/view/(*)/(.*)$ http://www.domain.com/$2 [R=301,L]
导致一个大500的错误。
网站是一个WordPress cms的裁决。
提前致谢!
在Michael Berkowski的回答之后添加。 我目前的wp规则是:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule>
500错误是(*)
的结果,其中*
(零个或多个)没有任何前缀作为限定符。 你可能打算使用(.*)
但是你真正需要的是[^/]+
把所有的字符都放到下一个/
Options +FollowSymLinks RewriteEngine On # Slight modification of the subdomain - must escape . RewriteCond %{HTTP_HOST} subdomain\.domain\.com$ [NC] # Note this may need to be ^view/ instead of ^/view # In htaccess context the leading / should probably not be there RewriteRule ^/view/([^/]+)/(.*)$ http://www.domain.com/$2 [R=301,L]
以上内容专门针对subdomain.domain.com
,但是由于您指定的是可变的,因此可以使用它来获取除www.domain.com
之外的所有子域:
Options +FollowSymLinks RewriteEngine On # Matches all subdomains except www. RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] RewriteRule ^/view/([^/]+)/(.*)$ http://www.domain.com/$2 [R=301,L]
如果没有这个,发布任何其他的重写规则(因为你提到这是WordPress,我希望你有其他的规则),因为顺序可能是重要的。
<Ifmodulee mod_rewrite.c> RewriteEngine On RewriteBase / # The new rules handle the subdomain redirect... RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC] RewriteRule ^view/([^/]+)/(.*)$ http://www.domain.com/$2 [R=301,L] # After which WordPress does its internal redirection RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </Ifmodulee>