RewriteRule 是如何工作的?

发布于 2024-09-15 02:15:49 字数 721 浏览 2 评论 0原文

我只是不明白:

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /

RewriteRule ^([a-z]+\-[0-9]+)/?$ $1/home/ [R]
RewriteRule ^[a-z]+\-([0-9]+)/(home|alone)/?$ /$2.php?id=$1 [L]
RewriteRule ^.*$ http://www.anotherdomain.com/ [R=301]

为什么总是处理最后一条规则(最后我的意思是重定向到 anotherdomain.com)?

我需要这样的东西:

http://mydomain.com/some-344 ---> http://mydomain.com/some-344/home/
http://mydomain.com/some-344/ ---> http://mydomain.com/some-344/home/
http://mydomain.com/some-344/home/ ---> home.php?id=344
http://mydomain.com/some-344/alone/ ---> alone.php?id=344
http://mydomain.com/anythingelse... --> http://www.anotherdomain.com/

谢谢!

I just don't get it:

Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /

RewriteRule ^([a-z]+\-[0-9]+)/?$ $1/home/ [R]
RewriteRule ^[a-z]+\-([0-9]+)/(home|alone)/?$ /$2.php?id=$1 [L]
RewriteRule ^.*$ http://www.anotherdomain.com/ [R=301]

why is the last rule (and by last I mean the redirection to anotherdomain.com) always processed?.

I need something like this:

http://mydomain.com/some-344 ---> http://mydomain.com/some-344/home/
http://mydomain.com/some-344/ ---> http://mydomain.com/some-344/home/
http://mydomain.com/some-344/home/ ---> home.php?id=344
http://mydomain.com/some-344/alone/ ---> alone.php?id=344
http://mydomain.com/anythingelse... --> http://www.anotherdomain.com/

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最单纯的乌龟 2024-09-22 02:15:49

最后一条规则始终会被处理,因为 ^.*$ 将始终匹配。虽然您在第二条规则上指定了 L 标志,但它可能并不像您期望的那样工作

确保在重定向到本地路径时包含前导斜杠也是一个好主意,并且在一般重定向时指定 L 标志,以便立即发生重定向。目前,一切都很顺利,但如果你看看幕后进行的处理,就会发现它的处理过程比必要的要混乱一些。

就您的实际问题而言,根据对服务器的原始请求调整包罗万象的重定向应该可以满足您的需求:

RewriteEngine on
RewriteBase /

RewriteRule ^([a-z]+\-[0-9]+)/?$ /$1/home/ [R,L]
RewriteRule ^[a-z]+\-([0-9]+)/(home|alone)/?$ /$2.php?id=$1

RewriteCond %{THE_REQUEST} !^[A-Z]+\s/[a-z]+\-[0-9]+/(home|alone)/?
RewriteRule ^.*$ http://www.anotherdomain.com/ [R=301,L]

The last rule is always processed, because ^.*$ will always match. While you've specified the L flag on the second rule, it probably doesn't work quite like you expect.

It's also a good idea to make sure when redirecting to a local path, you include a leading slash, and when redirecting in general, you specify the L flag so the redirect happens immediately. Currently, it all works out OK, but if you look at the processing going on under the hood, it's doing things a little more messily than necessary.

As far as your actual issue goes, conditioning the catch-all redirect based on the original request to the server should get you what you wanted:

RewriteEngine on
RewriteBase /

RewriteRule ^([a-z]+\-[0-9]+)/?$ /$1/home/ [R,L]
RewriteRule ^[a-z]+\-([0-9]+)/(home|alone)/?$ /$2.php?id=$1

RewriteCond %{THE_REQUEST} !^[A-Z]+\s/[a-z]+\-[0-9]+/(home|alone)/?
RewriteRule ^.*$ http://www.anotherdomain.com/ [R=301,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文