如何使用htaccess将现有目录重定向到子域?
在我的网站我有一个名为as.my
的目录,它实际上包含我网站上名为my.example.com
的子域的文件。如果有人访问www.example.com/as.my
,它们会显示子域。如果可能的话,我想要做的就是将以as.my
开头的任何URL重定向到子域,并从URL中去除as.my
。这可能吗?如何使用htaccess将现有目录重定向到子域?
我已经能够使用
Redirect Permanent /as.my http://my.example.com
的夹重定向,但如果有人访问http://www.example.com/as.my/test/
不正常工作。我也试图使用
RewriteRule ^as.my(/.*)$ http://my.example.com/$1 [L,NC,QSA,R=301]
但是,这也不能正常工作。有没有一种方法可以用来完成我正在尝试的? URL的行为如下:
http://www.example.com/as.my/ => http://my.example.com/
http://www.example.com/as.my/test/ => http://my.example.com/test/
谢谢任何帮助!
编辑:这是我的目录布局,以及每个内部的htaccess文件。
主目录包含example.com
的所有文件,以及目录as.my
,其中包含子域my.example.com
的所有文件。该htaccess
文件example.com
如下:
ErrorDocument 400 /pg.errors.php?eC=400
ErrorDocument 401 /pg.errors.php?eC=401
ErrorDocument 403 /pg.errors.php?eC=403
ErrorDocument 404 /pg.errors.php?eC=404
ErrorDocument 500 /pg.errors.php?eC=500
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase/
# Add WWW
RewriteCond %{HTTP_HOST} !^(my|www).
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
# Trailing Slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
# URL Rewrites (There are a few like this, but they're all the same)
RewriteRule ^about/$ about.php [L,NC,QSA]
RewriteRule ^contact/$ contact.php [L,NC,QSA]
</IfModule>
<Files .htaccess>
Order Allow,Deny
Deny From All
</Files>
的htaccess
文件中的as.my
目录如下:
ErrorDocument 400 /pg.errors.php?eC=400
ErrorDocument 401 /pg.errors.php?eC=401
ErrorDocument 403 /pg.errors.php?eC=403
ErrorDocument 404 /pg.errors.php?eC=404
ErrorDocument 500 /pg.errors.php?eC=500
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1/ [L,R=301]
# URL Rewrites (There are a few like this, but they're all the same)
RewriteRule ^help/$ help.php [L,NC,QSA]
</IfModule>
<Files .htaccess>
Order Allow,Deny
Deny From All
</Files>
<Files ~ "\.tpl$">
Order Allow,Deny
Deny From All
</Files>
You don't actually need mod_rewrite
to do this.你是在正确的轨道与永久重定向上。
RedirectMatch permanent ^as.my http://my.example.com/
注意使用RedirectMatch
代替Redirect
。根据the documentation,
该指令等同于重定向,但使用正则表达式,而不是简单的前缀匹配。
这意味着匹配将全局化为整个请求URI并将其全部重定向到子域。
感谢您的答案,但它似乎并没有工作。我假设我必须在我的一些htaccess的其他地方有一个问题。不过,我会继续尝试。 – 2014-10-03 18:19:17
通过“它似乎不工作”,你的意思是,“我得到了和以前一样的结果”? (其他地方的问题肯定会解释这一点,所以我会看看如何摆脱。) – pjmorse 2014-10-03 20:42:09
有此规则在/as.my/.htaccess
只是斜线规则后:
RewriteEngine On
RewriteBase /as.my/
RewriteCond %{HTTP_HOST} !^my\.example\.com$ [NC]
RewriteRule ^(.*)$ http://my.example.com/$1 [L,NC,NE,R=301]
'永久重定向HTTP /as.my:// my.example.com'? – 2014-10-03 16:43:32
@MarcB哎呀!这就是我之前的意图。我编辑了我的答案。 – 2014-10-03 16:48:37