htaccess在子目录中添加WWW和更改文件名

时间:2014-02-18 00:32:21

标签: apache .htaccess mod-rewrite redirect

我知道有类似的问题出现了,尽管经常without a working answer。我希望能有更好的运气!

我的根目录中有一个.htaccess文件,为所有内容添加“www”:

RewriteEngine on
RewriteCond %{SERVER_NAME} ^mysite.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]

这通常很好。我有一个子文件夹(/ myquiz /),其中旧的index.html文件已被index.php替换。我知道有/myquiz/index.html的外部链接,所以我想确保那些重定向。保留index.html并尝试从中重定向导致一些奇数行为,但在该目录中添加.htaccess适用于此:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R]

尝试按原样将index.html重定向加载到index.php,并在需要时添加WWW。但直接请求mysite.org/myquiz/index.php 添加WWW。

我尝试添加“RewriteEngine inherit”,但这导致调用被重定向到我的根文件夹。如果我想让一个子文件夹无法访问,但在这里没有帮助,这是一个很好的技巧。我还尝试将我的根.htaccess中的代码添加到我的子文件夹的.htaccess的开头,但这没有用。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您不需要在myquiz文件夹中添加另一个htaccess文件。这应该在站点根目录中的htaccess文件中工作。删除myquiz中的htaccess文件并尝试此操作。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite\.org
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=301,L]
RewriteRule ^myquiz/index\.html$ /myquiz/index.php [R=301,L]

除非您确定在配置文件中正确设置了名称,否则我不会使用%{SERVER_NAME}。然后它可以比HTTP_HOST更可靠,否则我会使用%{HTTP_HOST}

答案 1 :(得分:0)

如果您为inherit文件夹中的规则添加L标记,我认为myquiz会有效:

RewriteRule ^index\.html?$ /myquiz/index\.php [NC,R,L]

因此,它首先重定向,然后继承的规则(www)将被应用。

您也可以将两个规则放在同一个文件中:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.org$ [NC]
RewriteRule ^(.*)$ http://www.mysite.org/$1 [R=permanent,L]

RewriteRule ^myquiz/index\.html$ http://www.mysite.org/myquiz/index.php [R=permanent,L]