2012-12-29 93 views
1

我在我的網站下面的結構,我需要所有的流量重定向到一個子htaccess的重寫路徑子域除了目錄文件

結構:

domain.com/subdomain/ 
       .../folder1/folder/1.txt 
       .../folder1/folder/2.txt 
       .../folder2/folder/1.txt 
       .../folder2/folder/1.txt 
       .../index.php 

我想重定向所有

domain.com/subdomain/folder1/folder/1.txt ---> subdomain.domain.com/folder1/folder/1.txt 
domain.com/subdomain/folder1/folder/2.txt ---> subdomain.domain.com/folder1/folder/1.txt 
:除了在index.php或(domain.com/subdomain/)到一個子域

實施例的流量

domain.com/subdomain/index.php ---> No redirect 
domain.com/subdomain/ ---> No redirect 

這是我想出了:

Options +FollowSymlinks 
RewriteEngine on 
RewriteRule ^(.*)$ http://subdomain.domain.com/$1 [NC] 

這適用於所有的請求,但我想從這個重寫規則中排除兩個/ &的index.php

謝謝!

回答

1

您將需要比您在那裏多一點。您必須先用RewriteCond檢查域名。模式^subdomain/?(index\.php)?$應匹配請求到子域根或index.php有或沒​​有/。從技術上講,它也可以匹配無效的subdomainindex.php而不用/,但是這樣會導致404無論如何。

RewriteEngine On 
# If the requested domain isn't already the subdomain... 
RewriteCond %{HTTP_HOST} !^subdomain\. [NC] 
# Rewrite it to the subdomain 
# unless it is a request for index.php or/
RewriteRule ^subdomain/?(index\.php)?$ - [L] 
RewriteRule ^subdomain/(.*) http://subdomain.com/$1 [L,R=301,NC]