2015-05-04 56 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]