在.htaccess在这两个网站的根目录和子目录使用mod_rewrite [英] Using mod_rewrite in .htaccess in both website root and a subdirectory

查看:143
本文介绍了在.htaccess在这两个网站的根目录和子目录使用mod_rewrite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的两难困境:

在的.htaccess在我的网站的根目录:

In .htaccess in my website's root:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

在.htaccess中的子目录/富

In .htaccess in the subdirectory /foo

RewriteEngine On
RewriteRule ^page1\.html$ /foo/page2.html [R=301]

在第一个,我想,以确保所有的请求包括开始WWW。 第二,我对重定向请求page1.html在foo的子目录page2.html,也是在该子目录。

In the first, I'm trying to ensure all requests include the beginning www. In the second, I'm redirecting requests for page1.html in the foo subdirectory to page2.html, also in that subdirectory.

在我的浏览器,试图访问:

In my browser, trying to visit:

http://www.example.com/foo/page2.html &LT ; ==作品,好

http://www.example.com/foo/page2.html <== works, good

http://www.example.com/foo/page1.html &LT ; ==重定向到 http://www.example.com/foo/page2.html

http://www.example.com/foo/page1.html <== redirects to http://www.example.com/foo/page2.html, good

http://example.com/foo/page1.html &LT; ==重定向到 http://www.example.com/foo/page2.html ,好

http://example.com/foo/page1.html <== redirects to http://www.example.com/foo/page2.html, good

http://example.com/foo/page2.html &LT; ==没有重定向发生,坏

http://example.com/foo/page2.html <== no redirect occurs, bad

==>应重定向到:HTTP:// WWW example.com/foo/page2.html

==> Should redirect to: http://www.example.com/foo/page2.html

通过实验,似乎重定向规则,在网站的根才生效的请求页面.htaccess文件在子目录如果的子目录中不包含.htaccess文件,或它并指定一个重写规则确实需要为这个特殊要求的效果。

Through experimenting, it would seem the redirect rules in the .htaccess file in the website's root only take effect for requests to pages in that subdirectory IF that subdirectory does not contain a .htaccess file, or it does and specifies a rewrite rule that does take effect for this particular request.

有人能看到我在做什么错了?我怎样才能重写规则,坚持了www。在它是否失踪火 http://example.com/foo/page2.html

Can anyone see what I'm doing wrong? How can I get the rewrite rule that sticks the www. in if it's missing to fire for http://example.com/foo/page2.html?


感谢你跳,那工作!

有关的记录,然后我不得不改变重写规则的文件,在该网站的根目录为:

For the record, I then had to change the rewrite rule in the file in the site's root to:

RewriteRule ^.*$ http://www.example.com%{REQUEST_URI} [R=301,L]

这是蛮好的。谢谢!

Which is just fine. Thanks!

推荐答案

您是缺少富/的.htaccess 以下指令:

RewriteOptions inherit

比照。 文档

这篇关于在.htaccess在这两个网站的根目录和子目录使用mod_rewrite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆