mod_rewrite 与 GET 请求

发布于 2024-09-03 12:47:14 字数 887 浏览 3 评论 0原文

我的大部分网站上都有 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 技术交流群。

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

发布评论

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

评论(2

新一帅帅 2024-09-10 12:47:14

如果您只想将: 转换

search.php?keyword=KEYWORD

为:

search/?keyword=KEYWORD

您需要做的就是:

RewriteRule ^search/$ search.php [QSA]

QSA 标志意味着“查询字符串附加”,并将您通过 GET 请求的任何内容传递给 search.php:

search/?keyword=KEYWORDD
search/?name=value&name2=value2    

您可能还想查看 Apache MultiViews,它将每个 /foo 请求发送到它在 / 中找到的任何 foo.* 文件目录,尽管它们被认为是不好的

If you just want to transform:

search.php?keyword=KEYWORD

into:

search/?keyword=KEYWORD

all you need to do is:

RewriteRule ^search/$ search.php [QSA]

The QSA flag means "query string append", and passes to search.php whatever you request via GET:

search/?keyword=KEYWORDD
search/?name=value&name2=value2    

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.

岁月无声 2024-09-10 12:47:14
RewriteRule ^search/\?keyword=([^/.]+)/?$ search.php?search=$1

问号字符在正则表达式中具有特殊含义。你需要逃避它。

此外,点在字符类中没有特殊含义;您不需要转义它(您要求关键字不包含正斜杠和点)。

RewriteRule ^search/\?keyword=([^/.]+)/?$ search.php?search=$1

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文