繁体   English   中英

使用.htaccess将任何子域重定向到主域

[英]redirect any subdomain to main domain with .htaccess

我试图重定向任何直接尝试访问sub.example.com/login超过原始域/ URI example.com/login ,并从问题的数量我在关于这个确切的事情已经读过,它看起来很容易...

我不知道是否有发生的事情超越了预期的结果,但是我的.htaccess文件中具有以下规则,但是我仍然能够访问sub.example.com/login而无需进行重定向。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com/login$  [NC]
RewriteRule ^(.*)$ http://example.com/login$1        [L,R=301]

实际上,为了完全清楚起见,这是我整个.htaccess文件的内容。 也许其他人可以看到我所缺少的错误。

RewriteEngine On
RewriteBase /
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]

# redirect subdomain login attempts to main domain
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com/login$  [NC]
RewriteRule ^(.*)$ http://example.com/login$1        [L,R=301]

将Redirect子域登录重写条件和规则放在RewriteBase行之后。 那应该有助于解决您的问题。

RewriteEngine On
RewriteBase /

# Redirect all subdomains to example.com
RewriteCond %{HTTP_HOST} !^(.*)\.example\.com$ [NC]
# Redirect only bar & foo subdomains to example.com
#RewriteCond %{HTTP_HOST} !^(bar|foo)\.example\.com$ [NC]
RewriteRule ^ http://example.com/ [L,R]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM