2014-10-03 132 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

相關問題