ModRewrite if /search/ appears in URL

Hi,

I have a number of links such as /search/texas /search/colorado

I am trying to write a modrewite tool which will test the URL and if it contains /search/ then it will redirect to /search/index.php.

Any pointers?

Bit stuck!

K

cdk,

Oh, more meat for DK’s meatgrinder!

What is that supposed to do? All I can see is that it matches search/ followed by {garbage} and assumes that search.php/ is a directory within which {garbage} can be found (and served). Okay, that’s possible with Options +MultiViews (which serves search.php which must then parse the path for {garbage} and do something useful with it). :nono:

Why not start at the beginning (again) and code “I am trying to write a modrewite tool which will test the URL and if it contains /search/{state} then it will redirect to /search/index.php.” Hmmm, that means that you were close but …

RewriteEngine on
RewriteRule ^search/(.+[COLOR="Magenta"]/[/COLOR])$ search.php [L]

Okay, okay, I read between the lines to dig out that you’re trying to match state names and will probably use that in your search/index.php file to pull state info from a db. If you’re only trying to match a couple of states, e.g., texas and colorado, then the following would be more appropriate:

RewriteEngine on
RewriteRule ^search/(texas|colorado)[COLOR="Magenta"]/[/COLOR]$ search.php [L]

Enumerating the strings which constitute an acceptable match is the best thing that you can do (to protect your code).

Regards,

DK

Thanks David,

I’ve got it to redirect the main directory but not the sub directories:

RewriteRule ^search/(.*)$ /search.php/$1 [L]

Any advice?

Kyle

Hi,

If the URL contains /search/ then it needs to redirect to search.php. From the search.php page the PHP code will then process the page based on the URL - the PHP code will ready the URL.

So if url is /search/texas/ then it will go to search.php and does not need to pass any variables to it.

If the url is /something/ then nothing needs to happen. It is only if a /search/ directory exists.

Does that make sense

K

cdk,

Have a look at the tutorial linked in my signature. It addresses problems like that … and I’ll be here to answer questions you might have.

Regards,

DK

CDK,

Well, not really. What is “something”? Can it be “search”? If not, then the “search/” in the regex will cause it NOT to match “something” and there will be no redirection.

Worse, you’ve changed your redirection from search/index.php to search.php. Which is it?

Finally, is PHP reading the Apache variable {THE_REQUEST} to get the requested URI (not the redirection)? That makes this example trivial!

Regards,

DK