简单的mod重写问题 [英] Simple mod rewrite question

查看:55
本文介绍了简单的mod重写问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我当前的.htaccess文件:

Here is my current .htaccess file:

RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

如您所见,对 http://domain.com 的请求将转到 http://domain.com/foo ,请注意,不作为文件或文件夹存在,它由rails处理.我该怎么做呢?请注意,我已经尝试了以下方法,但它不起作用:

As you can see, requests to http://domain.com go to http://domain.com/index.html. I want to change this so that they go to http://domain.com/foo, please note that does not exist as a file or folder, it is handled by rails. How do I do this? Note that I have tried the following and it doesn't work:

RewriteRule ^$ foo [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ dispatch.fcgi [QSA,L]

谢谢!

推荐答案

将第一行更改为以下内容可能很安全:

You're probably safe to just change the first line to this:

RewriteRule ^$ foo [QSA,L]

L标志告诉mod-rewrite在该规则之后不应应用任何其他规则.现在的问题是,第二条规则在第一条规则之后应用,而您最终到达的是"foo.html"而不是"foo",对吧?

The L flag tells mod-rewrite that it shouldn't apply any other rules after that one. The problem right now is that the second rule gets applied after the first one, and you end up at "foo.html", instead of "foo", right?

您尝试发送到"foo"和原始重定向到"index.html"之间的区别在于,第二条规则适用于不包含句点的请求.因此,当第一条规则重定向到"index.html"时,使用后,第二条规则不再有效.但是,由于您没有重定向到其中带有句点的位置,因此第二条规则将在第一个规则之后应用,因此您将获得双重重定向.

The difference between you trying to send to "foo" and the original redirect to "index.html" is that the second rule applies to requests that do not include a period. So when the first rule was redirecting to "index.html", after it was used, the second rule was no longer valid. However, now that you're not redirecting to a location with a period in it, the second rule gets applied after the first one, so you get a double-redirect.

此外,您也许可以从第一行删除QSA标志,但这取决于您的站点.如果有人访问http://domain.com/?user=fred之类的站点,您是否要将其发送到http://domain.com/foo?user=fred或仅发送给http://domain.com/foo?如果不需要附加的 Q uery S tring A ,则可以删除QSA标志,而只需:

In addition, you may be able to drop the QSA flag from the first line, it depends on your site though. If someone accesses the site like http://domain.com/?user=fred, do you want to send them to http://domain.com/foo?user=fred, or just http://domain.com/foo? If you don't need the Query String Appended, you can drop the QSA flag, and just have:

RewriteRule ^$ foo [L]

这篇关于简单的mod重写问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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