2013-03-27 61 views
0

如果未在URL請求中指定子文件夾,則需要將我的域重定向到子文件夾。未指定Apache重定向域到文件夾

例如,

a)如果請求來自「domian.com」或「www.domain.com」,我想將其重定向到「www.domain.com/page」。

b)但是,如果請求來自「domain.com/page2」或「www.domain.com/page2」,我不希望請求被重定向到「www.domain.com/page」。

我一直在虛擬主機中嘗試一些排列組合,但沒有運氣。以下是我的代碼:

<VirtualHost *:80> 
ServerName domain.com 
ServerAlias www.domain.com 

<Directory /> 
    Options Indexes FollowSymLinks MultiViews 
    AllowOverride All 
    Order allow,deny 
    allow from all 
    <IfModule mod_rewrite.c> 
     Options +FollowSymLinks 
     Options +Indexes 
     RewriteEngine On 
     RewriteBase/
     RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ 
     RewriteCond %{REQUEST_URI} !^/$1 
     RewriteRule ^(.*)$ /Page [L] 
    </IfModule> 
</Directory> 

ProxyPreserveHost On 
ProxyPass  /http://www.domain.com:8080/ 
ProxyPassReverse/http://www.domain.com:8080/ 
</VirtualHost> 

任何指針都會有幫助。

回答

1

很可能,你試試這個:

RewriteEngine On 
    RewriteBase/
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com [NC] 
    RewriteCond %{REQUEST_URI} ^$ [NC] 
    RewriteRule .* /Page [L] 
+0

這不起作用! – 2013-03-27 09:07:44

+0

爲什麼在有URI路徑時使用規則? – 2013-03-27 09:14:30

+0

我需要的是,如果我登陸domain.com或www.domain.com,它應該被重新定向到www.domain.com/Page,如果我登錄www.domain.com/Page2,它不應該是重定向。 – 2013-03-27 09:22:29

0

RewriteCond%{REQUEST_URI} !^/$1排除每一個請求,因此從未使用過RewriteRule

如果您只想重寫根目錄,請改用特定的regular expression^/$。您還必須包裹的規則在適當的<Directory>,因爲一切都封閉適用於文件系統目錄, URL路徑

<Directory /path/to/documentroot> 
    RewriteEngine On 
    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ 
    RewriteRule ^$ /Page [L] 
</Directory> 
+0

你好奧拉夫,上面的規則將重定向到/頁面,而不管在URL中是否提供了子文件夾。我需要的是選擇性重寫。 此外,我嘗試了上述規則,它不會重新驅動到/ Page也。 – 2013-03-27 09:08:22

+0

@AkhileshAggarwal它只會重寫根目錄。如果您指定了任何網址路徑,例如'www.domain.com/abc',它將*不*重寫到'/ Page',而是用'/ abc'來代替。 – 2013-03-27 09:12:51

+0

我測試了這個規則,它不是根目錄的重定向:( – 2013-03-27 09:15:44

相關問題