htaccess重定向转到错误的URL

时间:2016-01-29 09:16:19

标签: wordpress .htaccess url-redirection

我在.htaccess中设置了以下规则:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    // Redirect old blog URLs to new location
    RewriteCond %{HTTP_HOST} ^www.objectsanduse.com/blog/$
    RewriteRule ^(.*)$ http://www.objectsanduse.com/$1 [R=301,L]

    // Wordpress rewrite rules
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

网址http://www.somedomain.com/blog/2016/01/19/some-post/正确地重定向到http://www.somedomain.com/2016/01/19/some-post/

http://www.somedomain.com/blog重定向到http://www.somedomain.com//2015/03/30/some-other-post

2 个答案:

答案 0 :(得分:2)

如果我理解正确,你只需要一条规则:

RewriteRule ^(blog\/?)(.*) http://%{HTTP_HOST}/$2 [R=301,L]

此规则采用以blog开头的任何旧URI(可选地后跟/

并捕获随后的所有内容(其中包括&#34;什么都没有&#34;)。

然后它重写URI,包含第二个捕获组但没有前面的blog/

答案 1 :(得分:1)

您在此规则中遇到问题:

// Redirect old blog URLs to new location
RewriteCond %{HTTP_HOST} ^www.objectsanduse.com/blog/$
RewriteRule ^(.*)$ http://www.objectsanduse.com/$1 [R=301,L]

由于%{HTTP_HOST}变量仅匹配没有URI的域名。您可以通过播放来修复该规则:

// Redirect old blog URLs to new location
RewriteCond %{HTTP_HOST} ^www\.objectsanduse\.com$
RewriteRule ^blog(/.*)?$ http://%{HTTP_HOST}$1 [NC,R=301,L]