如何使用 mod_rewrite 来“移动”站点目录出现到根目录而不实际移动文件?找出我的代码中的错误!
所以,我在网站上有我想要的页面,例如;
www.example.com/shop/index.php
(或者 /shop/ 下的任何其他页面。)现在显示为
www.example.com/index.php
但没有实际移动文件,这些文件仍然位于 /shop/ 下,但现在可以通过 / 的 url 访问
似乎很简单,我可以做到这一点其本身使用 RewriteRule,非常标准。第二个要求是保持旧内容的链接正常工作,所以我想要一个类似的东西,但在另一个方向,但使用 301 外部重定向(而不是重写),即
www.example.com/shop/index.php
应该 301 重定向到;
www.example.com/index.php
同样,它本身就足够简单了。但是将两者放在一起,您在访问页面时会出现一个可爱的“此网页有重定向循环”错误。但是,我不明白为什么,因为一个规则是重写,一个是 301 重定向,并且它们都有 L 标志,所以我认为没有更多规则被处理。所以,我对 mod_rewrite 的理解已经到了极限。
为了测试并避免为访问者弄乱我的网站,我使用目录 /blob/ 和 /b/,其中 /b/ 是new 目录。我目前拥有的提供重定向循环的代码是;
RewriteRule ^blob/(.*)$ "http\:\/\/www\.example.com\/b\/$1" [R=301,NC,L]
RewriteRule ^b/(.*)$ /blob/$1 [NC,L]
我猜这是因为一旦第二条规则执行,第一条规则就会再次执行,如果使用“L”标志,为什么会出现这种情况?我应该检查或更改什么条件,以便只有在原始请求针对的是 /blob/ 而不是重写的 URI 时才激活重定向规则。
So, I have at site where I want pages such as;
www.example.com/shop/index.php
(or any other page under /shop/ for that matter.) to now appear as
www.example.com/index.php
but without actually moving the files, which are still located under /shop/ but are now accessed via urls at /
Seems simple enough, and I can do that on its own using a RewriteRule, pretty standard. The second requirement is to keep the links to the old content working, so I want a similar thing but in the other direction, but using a 301 external redirect (not rewrite), i.e.
www.example.com/shop/index.php
should 301 redirect to;
www.example.com/index.php
Again, simple enough on its own. But put the two together and you it gives you a lovely "This web page has a redirect loop" error when accessing a page. However, I don't understand why because one rule is a rewrite and one is a 301 redirect and they both have the L flag so I thought no more rules were processed. So, I am at the limit of my understanding of this mod_rewrite stuff.
To test, and avoid mucking up my site for people visiting, I am using the directories /blob/ and /b/ with /b/ being the new directory. The code I currently have that gives the redirect loop is;
RewriteRule ^blob/(.*)$ "http\:\/\/www\.example.com\/b\/$1" [R=301,NC,L]
RewriteRule ^b/(.*)$ /blob/$1 [NC,L]
I guess its because the first rule is being executed again once the second has, why is this if the 'L' flag is used? And what condition should I check or change to make so that the redirect rule only gets activated if the original request is for /blob/ not the rewritten URI.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无需额外的查询参数,您可以在 .htaccess 文件中使用以下规则:
/shop/foo< 的任何请求发送
301
/code> 到浏览器中的/foo
,这是一个外部重定向/foo
到/shop/goo
而不更改浏览器中的 URL,从而确保您的实际文件是从$DOCUMENT_ROOT/shop/
目录提供的。Without any need of an extra query parameter you can use following rules in your .htaccess file:
301
to any request from/shop/foo
to/foo
in the browser, which is an external redirect/foo
to/shop/goo
without changing URL in the browser thus making sure your actual files are served from$DOCUMENT_ROOT/shop/
directory.尝试以下操作(我使用了您的实际网址示例):
您遇到的问题是,当
mod_rewrite
执行rewrite
时,它实际上是在执行内部重定向< /code>,这会导致规则再次应用。因此,这意味着您必须防范这种情况,在本例中,我已将参数
rewrite
添加到重定向检查的 URL。希望有帮助。
Try the following (I used your actual url examples):
The issue you are running into is that when
mod_rewrite
does therewrite
it is actually performing anInternal Redirect
, which causes the rules to applied again. So, this means you have to protect against that, in this case, I've added a parameterrewrite
to the URL that the redirect checks for.Hope that helps.