.htaccess 301将子子目录URL重定向到固定的子目录URL

时间:2016-07-07 18:34:12

标签: .htaccess redirect mod-rewrite

我需要重定向以下所有网址:

/blog/blog/this-is-an-example-page/
/blog/blog/hello-how-are-you/
/blog/blog/2015/04/29/old-article/
/blog/blog/2014/09/12/

以下目录:

/blog/

只是丢失了URL的其余部分。因此,/blog/blog/this-is-an-example-page/不会转到/blog/this-is-an-example-page/,而是/blog/。这是我的.htaccess文件中应该执行此操作的行,但它不起作用:

RewriteRule ^blog/blog/(.*)$ /blog/$1 [R=301,L,NC]

这是完整的.htaccess文件内容:

Options +FollowSymLinks
RewriteEngine On

# Force https and www
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.easterisland.travel/blog/$1 [R,L]


# BEGIN WordPress
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
# END WordPress

RewriteRule ^blog/blog/(.*)$ /blog/$1 [R=301,L,NC]

此文件位于/ blog / dir。

我做错了什么?为什么不起作用?

2 个答案:

答案 0 :(得分:1)

您无需与htaccess文件所在的文件夹匹配,请尝试:

RewriteRule ^blog/(.+)$ /blog/$1 [R=301,L,NC]

试试这个htacess:

Options +FollowSymLinks
RewriteEngine On
    # Force https and www
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.easterisland.travel/blog/$1 [R,L]
  # BEGIN WordPress
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteRule ^blog/(.+)$ /blog/ [R=301,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
# END WordPress

答案 1 :(得分:1)

在其他规则之前保留重定向规则并修复您的www + https规则:

Options +FollowSymLinks
RewriteEngine On

# Force https and www
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.easterisland.travel/$1 [R=301,L,NE]

RewriteRule ^blog/blog/(.*)$ /blog/ [R=301,L,NC]

# BEGIN WordPress
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
# END WordPress

确保在测试此更改时完全清除浏览器缓存。