2014-10-03 79 views
0

在我的网站我有一个名为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> 
+0

'永久重定向HTTP /as.my:// my.example.com'? – 2014-10-03 16:43:32

+0

@MarcB哎呀!这就是我之前的意图。我编辑了我的答案。 – 2014-10-03 16:48:37

回答

0

You don't actually need mod_rewrite to do this.你是在正确的轨道与永久重定向上。

RedirectMatch permanent ^as.my http://my.example.com/ 

注意使用RedirectMatch代替Redirect。根据the documentation

该指令等同于重定向,但使用正则表达式,而不是简单的前缀匹配。

这意味着匹配将全局化为整个请求URI并将其全部重定向到子域。

+0

感谢您的答案,但它似乎并没有工作。我假设我必须在我的一些htaccess的其他地方有一个问题。不过,我会继续尝试。 – 2014-10-03 18:19:17

+0

通过“它似乎不工作”,你的意思是,“我得到了和以前一样的结果”? (其他地方的问题肯定会解释这一点,所以我会看看如何摆脱。) – pjmorse 2014-10-03 20:42:09

0

有此规则在/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] 
+0

感谢您的答案,但它似乎并没有工作。除非我在'as.my'之后加入了一些东西,否则不会发生重定向,然后它只是将URL重新定向到主站点。 – 2014-10-03 18:17:21

+0

你有更多的规则或只是使用这个规则?这个.htaccess位于哪里? – anubhava 2014-10-03 18:19:37

+0

正如我在我的回答中所建议的,这个规则应该在根目录下.htaccess – anubhava 2014-10-03 18:25:00

相关问题