Cant get a url rewrite to work

Ok here is the longwinded story. We have a search box with two fields, one is for zip one is for city state – it searches the same database. There is default text in each box, but when you focus the box, the text disappears (but the default remains in the other box).

The site was built passing variables by POST, but that does not allow bookmarkable links. So I turned them into GETS; problem is the default text in the other box is passed with the parameter. For example, in the case of the zip being passed, you get this

http://localhost/lcp/carpetlist.php?zip=89109&city=Enter+City+and+State+(ex:+Houston,+TX)&x=0&y=0

In the case of the city and state being passed, you get this:
http://localhost/lcp/carpetlist.php?zip=Enter+Five+Digit+Zip+Code+(ex:+89109)&city=las+vegas,+nv&x=0&y=0

This is the rewrite rule I’m trying to use in the case of the zip being passed

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^carpetlist/location/([0-9]{5})$ /carpetlist.php?zip=$1city=Enter+City+and+State+(ex:+Houston,+TX)&x=0&y=0

i want the url to look like this :
http://localhost/lcp/carpetlist/location/89109

I have read tons and tons of tutorials over the weekend, and what I have seems like it should work. I have confirmed that mod_rewrite is turned on, as I have already done a simple alias with it. I can’t believe this is so difficult.

Thanks in advance for any help.

ZB,

First, WELCOME to SitePoint’s Apache forum!

Second, you have (as I see it) three choices (in order of my preference):

  1. Divide your form into two forms so you only get the value you want. This will save a lot of other problems and keep your handler script from having to deal with incompatible data (zip code not being correct for city/state).

  2. Use JavaScript to check the contents of the two (three?) fields before submitting and, if the content matches the default, change the content to “” so that value is not included in the GET submission.

  3. Do the same thing as #2 with mod_rewrite. Because mod_rewrite is not a scripting language, you’re better off with option #2. That being said, however:

# .htaccess in the lcp directory
# If this is a VirtualHost, use http://lcp/

RewriteEngine on

# Handle Zip code request
RewriteCond %{IS_SUBREQ} false
# to prevent looping
RewriteCond %{REQUEST_URI} zip=(\\d{5})
RewriteRule ^carpetlist\\.php$ carpetlist/location/%1 [R=301,L]

RewriteRule ^carpetlist/location/([0-9]{5})$ carpetlist.php?zip=$1 [L]
# without the leading / in the regex, this is Apache 2.x specific
# regex is PERFECT! :tup:
# I doubt your carpetlist.php script needs the "garbage" for city and state 
#    so I removed it from the redirection.
# The Last flag tells Apache you want it to process that redirection

# Handle city, state request
# This I leave for you to figure out ...

Okay, that was a two-step process to get BACK to your

As for having ensured that mod_rewrite is enabled because you have “already done a simple alias with it,” the Alias directive is from mod_alias, a part of Apache’s core so it has NOTHING to do with mod_rewrite being enabled. Have a look at my signature’s tutorial to see how to REALLY ensure mod_rewrite is enabled.

Regards,

DK

OK well I decided to create two differet searches, one for the zip and one for the city,state. I installed the code into the .htaccess file and nothing. I’ve used the test you said to use on your site to make sure mod_rewrite is installed and enabled, so I know it’s working.

Any more suggestions/things to check?

This is really weird, I’ve tried to use mod_rewrite functionality a number of times over the past few years and I’ve never been able to make it happen. It doesn’t make any sense.

ZB,

Suggestions:

  1. Post your code

Since you’ve done the test and it ensures not only that mod_rewrite is enabled but that your host has allowed your .htaccess to use it and you’ve tested that it does work, that part is done!

Regards,

DK

Thanks for your help so far dklynn:

I’m not sure which code to post. The mod_rewrite I used is the code you gave me above. Here are examples of the urls
Zip: http://localhost/lcp/carpetlist.php?zip=89109
City: http://localhost/lcp/carpetlist.php?city=las+vegas%2C+nv

ZB,

Quite obviously, the code I gave you was NOT designed to take care of the city case as I left that for you to do. I am glad, though, that you took the “simple route” to resolve the problem (because JavaScript may be disabled for some users).

# .htaccess [COLOR="Red"]in the lcp directory[/COLOR]

RewriteEngine on

# Handle Zip code request
RewriteCond %{IS_SUBREQ} false
RewriteCond %{REQUEST_URI} zip=([COLOR="Magenta"]\\d[/COLOR]{5})
RewriteRule ^[COLOR="RoyalBlue"]/?[/COLOR]carpetlist\\.php$ carpetlist/location/%1 [R=301,L]

RewriteRule ^[COLOR="RoyalBlue"]/?[/COLOR]carpetlist/location/([COLOR="Magenta"][0-9][/COLOR]{5})$ carpetlist.php?zip=$1 [L]

[COLOR="Red"]# Handle city, state request
# This I leave for you to figure out ...[/COLOR]

I’ve added the /? after the start anchor as directory level mod_rewrite works a little differently than in the DocumentRoot.

The two magenta bits are identical as far as regex is concerned - for consistency, use the same code at both locations (I didn’t want to change it without first explaining why).

Now, this will work on the zip case. Where is your code for the City case?

Hint: The comma is a “Reserved Character” (see http://www.ietf.org/rfc/rfc2396.txt) so the trick you used to add/encode it in your City string needs to be UNDONE - you can grab the state two letter identifier in your handler script by capturing the last two characters of the $_GET[‘city’] string. Better yet, separate it into two parts in the form, city and state keys.

Regards,

DK