views:

30

answers:

2

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

+1  A: 
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).

Artefacto
Thanks, I figured there was something like that, but I'm still having problems. I'm still getting the saem error page. I'm posting my entire .htaccess code, because maybe I'm doing something else wrong.
WillyG
@iMaster Are your sure you want `/?` at the end of the query string? I'm not even sure `/` and `?` can be in a query string.
Artefacto
My modified .htaccess has been posted above. I'm still not getting any results.
WillyG
+1  A: 

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.

Victor Stanciu
Awesome! Thanks!
WillyG
I wish people would stop linking to that article. It's a misunderstanding on how PHP should be configured in apache (hint: it's not `Addtype application/x-httpd-php .php`)
Artefacto