RewriteRule 是如何工作的?
我只是不明白:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最后一条规则始终会被处理,因为
^.*$
将始终匹配。虽然您在第二条规则上指定了L
标志,但它可能并不像您期望的那样工作。确保在重定向到本地路径时包含前导斜杠也是一个好主意,并且在一般重定向时指定
L
标志,以便立即发生重定向。目前,一切都很顺利,但如果你看看幕后进行的处理,就会发现它的处理过程比必要的要混乱一些。就您的实际问题而言,根据对服务器的原始请求调整包罗万象的重定向应该可以满足您的需求:
The last rule is always processed, because
^.*$
will always match. While you've specified theL
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: