mod_rewrite 与 GET 请求
我的大部分网站上都有 mod_rewrite 工作。现在我有一个搜索,通常会指向
search.php?keyword=KEYWORD
“我正在尝试将其重写为”
search/?keyword=KEYWORD
只是为了使其更干净一点。这是我的 mod_rewrite。 (还有其他规则,我只是发布了不起作用的规则。)
RewriteRule ^search/?keyword=([^/\.]+)/?$ search.php?search=$1
当我在地址栏中输入我希望的搜索方式时,我得到一个页面,告诉我它是一个“损坏的链接”(我'我猜这相当于 Chrome 的 404 错误)。那么我做错了什么?我认为问题在于“=”或“?”登录规则(第一部分),因为当我取出 ?keyword= 部分时,它就起作用了。这有道理吗?
编辑:这是我完整的 .htaccess 代码:
RewriteEngine on
RewriteRule ^$ index.php
RewriteRule ^thoughts$ archives.php
RewriteRule ^thoughts/$ archives.php
RewriteRule ^about$ about.php
RewriteRule ^about/$ about.php
RewriteRule ^search/\?keyword=([^/]+)$ search.php?search=$1
RewriteRule ^tags/([^/]+)$ tags.php?tag=$1
RewriteRule ^thoughts/([^/]+)$ post.php?title=$1 [L]
仍然收到错误页面...
I have mod_rewrite working on most of my site. Right now I have a search that normally would point to
search.php?keyword=KEYWORD
And I'm trying to rewrite that to
search/?keyword=KEYWORD
Just to make it a little bit cleaner. So here's my mod_rewrite. (There are other rules I'm just posting the one that isn't working.)
RewriteRule ^search/?keyword=([^/\.]+)/?$ search.php?search=$1
When I type a search in the address bar way I want it to be, I get a page telling me its a "broken link" (I'm guessing that that's Chrome's equivalent of a 404 error). So what am I doing wrong? I think that the problem is the '=' or the '?' sign in the rule (the first part) because when I take the ?keyword= part out, it works. Does that make sense?
EDIT: This is my full .htaccess code:
RewriteEngine on
RewriteRule ^$ index.php
RewriteRule ^thoughts$ archives.php
RewriteRule ^thoughts/$ archives.php
RewriteRule ^about$ about.php
RewriteRule ^about/$ about.php
RewriteRule ^search/\?keyword=([^/]+)$ search.php?search=$1
RewriteRule ^tags/([^/]+)$ tags.php?tag=$1
RewriteRule ^thoughts/([^/]+)$ post.php?title=$1 [L]
Still getting an error page...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只想将: 转换
为:
您需要做的就是:
QSA 标志意味着“查询字符串附加”,并将您通过 GET 请求的任何内容传递给 search.php:
您可能还想查看 Apache MultiViews,它将每个 /foo 请求发送到它在 / 中找到的任何 foo.* 文件目录,尽管它们被认为是不好的。
If you just want to transform:
into:
all you need to do is:
The QSA flag means "query string append", and passes to search.php whatever you request via GET:
You may also want to check out Apache MultiViews, which sends every /foo request to any foo.* file it finds in the / directory, although they are considered bad.
问号字符在正则表达式中具有特殊含义。你需要逃避它。
此外,点在字符类中没有特殊含义;您不需要转义它(您要求关键字不包含正斜杠和点)。
The question mark character has special meaning in a regex. You need to escape it.
Additionally, the dot has no special meaning when inside a character class; you need not escape it (you're requiring that keyword contain no forward slashes and dots).