.htaccess重定向url,不包括某些子目录

时间:2015-04-20 18:38:14

标签: apache .htaccess

我尝试将我的WordPress多站点根网站重定向到另一个网页,但我仍然希望能够访问/wp-admin/wp-login/wp-json

当我转到浏览器时,我希望url.com重定向到newurl.com除非它最后有一个上述目录。

这是我提出的,但当我进入浏览器并输入url.com时,它会像往常一样加载页面。我哪里错了?

注意:底部的块是重定向应该发生的位置。

Options FollowSymlinks
RewriteEngine On
RewriteBase /

# Force SSL / HTTPS
# Note: X-Forwarded-Proto is required for AWS's Elastic Load Balancing.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

# This is the block that should redirect, but isn't.
RewriteCond %{HTTP_HOST} ^url\.com$ [NC]
RewriteCond %{REQUEST_URI} !^(/wp-admin$|/wp-login$|/wp-json$)
RewriteRule ^ http://newurl.com/ [R=301]

2 个答案:

答案 0 :(得分:1)

这对我来说很好。我在条件上更改了一些内容并添加了L标志,以确保在满足规则时它停止。确保正在读取.htaccess文件。您可以验证您的apache配置文件是否为AllowOverride All,或者它无法读取您的htaccess文件。

Options -FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^url\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/(wp-admin|wp-login|wp-json)$
RewriteRule ^ http://newurl.com/ [R=301,L]

答案 1 :(得分:1)

你的规则工作正常 你没有看到它,因为它在htaccess中的位置从未达到过。

实际上,您需要将规则放在<{strong> default - 最后一条规则之前,否则永远不会执行。

Options FollowSymlinks
RewriteEngine On
RewriteBase /

# Force SSL / HTTPS
# Note: X-Forwarded-Proto is required for AWS's Elastic Load Balancing.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

# This is the block that should redirect, but isn't.
RewriteCond %{HTTP_HOST} ^url\.com$ [NC]
RewriteCond %{REQUEST_URI} !^(/wp-admin$|/wp-login$|/wp-json$)
RewriteRule ^ http://newurl.com/ [R=301]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]