2015-05-04 123 views
1

我想重定向訪問mysite.com/mydir/,以mysite.com/mydir/index.html重定向訪問根目錄

還有就是MYDIR內的.htaccess配置文件,但它不」牛逼按預期方式工作:

RewriteCond %{REQUEST_URI} ^/ 
Rewriterule ^(.*)$ index.html [L,R=301] 

現在,當我去mysite.com/mydir/,我重定向到:

mysite.com/mydir/C:/wamp/www/mysite/index.html 

和使用。如果改爲:

Rewriterule ^(.*)$ /index.html [L,R=301] 

我重定向到

mysite.com/index.html 

只有工作是事:

Rewriterule ^$ /mydir/index.html [L,R=301] 

,但我不想硬編碼mydir的,因爲這可能的.htaccess在另一個目錄中。這可以通過模塊化方式解決嗎?

+0

你試過重寫規則^(*)$ $ 1/index.html的[L,R = 301] – saml

回答

0

的index.html之前,您可以用下面的嘗試(0存儲在文件):

RewriteEngine on 
RewriteBase /mydir/ 

RewriteCond %{REQUEST_URI} ^/mydir/$ [NC] 
RewriteRule^index.html [R=301,L] 
+0

是的,它的工作原理,謝謝 – Zed

+0

不客氣。 –

0

我一直沒能對此進行測試,因爲我沒有Apache的安裝在本地做,但問題是,你是不是附加在括號組到url的開始,如果你嘗試

Rewriterule ^(.*)$ $1/index.html [L,R=301] 

那麼就應該將前面加上什麼都在你的URL開始的字符即MYDIR/

+0

可惜這重定向到mysite.com/index.html,我試過了。還添加/,如下所示:/$1/index.html,但它會重定向到:mysite.com//index.html(2 slashesh) – Zed

+0

對不起,我打算把Rewriterule ^(。*)$ $ 1index.html [L,R = 301]。我已經在http://htaccess.madewithlove.be/上測試過了 - 這還不行嗎? – saml

+0

我得到重定向到mysite.com/mydir/C:/wamp/www/mysite/index.html ,這很奇怪,它可能是WAMP配置的東西? – Zed

0

試試這個

RewriteCond %{REQUEST_URI} /$ 
Rewriterule (.*) http://%{HTTP_HOST}%{REQUEST_URI}index.html [L,R=301]