Mod rewrite

Hey Guys,

I’m at my wits end here , I normally like to work things out on my own but this has me well and truly beaten here…
I’m trying to mod rewrite my urls that contain pluses…

http://domain.com/search.php?q=can+be+any+length

to

http://domain.com/can-be-any-length.html

Any help would be really appreciated becaus rewriting the + php is not an option

DK,

I stand corrected.
I was assuming the situation where the is a form on the website with a GET action to produce an URL like [noparse]http://www.example.com/?q=my%20search%20terms[/noparse], in which case you need to use urldecode().

However, since this assumption is incorrect, your code (str_replace(‘-’, ’ ', $_GET[‘q’])) is indeed correct.

Uh, almost there! IMHO, JJ will need to convert the -'s to {space}'s once in his search script. Because that’s trivial,

$q = str_replace('-',' ',$q);

Regards,

DK

Hi Jay,

Welcome to Sitepoint forums!

There is not only a better but a best forum to ask such questions where some gurus are waiting for you to answer ;).

If I try, I would do something like this http://www.domain.com/search/can-be-any-length.html so that it is easier to track the things after the domain:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^search/(.*?)\\.html/?$ search.php?q=$1 [L]

I am not an expert and this is not well tested either.

Better someone (moderator) move this question to apache configuration forum!

Sorry, but no


$q = urldecode($_GET['q']);

You can remove


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

As they are unnecessary checks (for this particular code at least).

Remon,

Can’t I assume that JJ is smart enough to get the $q from the $_GET array before replacing -'s with spaces?

THEN, how does urldecode convert -'s to spaces? :nono:

Okay, I cheat! I know the answer to both as I do this regularly in my scripts.

Regards,

DK

Thanks that worked buddy!

ps I’ve been here from 2005 , used to post a bit but my post seem to have disappeared :wink:

Remon,

Interesting. I’ve never seen a GET form which could subvert a browser’s standard conversion of spaces to +'s.

If that were to happen, you’d have been correct that the urldecode() function would “fix” that problem and your code is a one-liner to read the GET value and replace the encoded spaces with actual space characters.

Interesting thought, though.

Regards,

DK