开发者

mod_rewrite with GET requests

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 
开发者_如何学CRewriteRule ^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...


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜