Good Ol mod_rewrite


#1

Please help!
I need some answers to my question in a noob format please. Since I have tried so many things to get this to work I must be missing some vital step.
Thanks!

Ok, I have tried to research this on my own so please spare me the simple page links to search results on GET Method or Mod_Rewrite.
I need to rewrite a url from a get method search form like this.

State:
City:

Where a user can enter a city name and a state code. Such as Seattle WA and currently it returns it in a query string (obviously)

What steps do I need to take to rewrite this to rewrite the url as
www.domain.com/state/wa/city/seattle.html
Which is where the page will reside.
I have messed around with my .htaccess file countless times trying various codes using all types of things (and from rewrite code generators) and nothing happens.
Do I need a php file to process it?
Any code would be a huge help!


#2

so there’s actually a file at state/wa/city/seattle.html? if that’s the case, mod_rewrite does the opposite of what you’re looking for.

instead, before any html in your form page, you want something like this in php:

<? if($_GET['state'] && $_GET['city']) { header('Location: http://' . $_SERVER['HTTP_HOST'] . '/state/' . $_GET['state'] . '/city/' . $_GET['city'] . '.html'); die; } ?>

you should probably add some error checking, but essentially that checks if there are values for city and state, and if so it redirects to http://www.example.com/state/wa/city/seattle.html instead of showing the form again. also if the user is choosing “Seattle” but you need to go to seattle.html, you’d want to use strtolower() before putting the $_GET value in the URL for the Location header.

track7 - my dream-hosted site


#3

Ok so here is the page i want to go to
http://yourplaceforhomes.com/state/wa/city/seattle.html
and I have the form on


when you fill out the form with wa and seattle it returns a query string of

I entered the code you provided and nothing changed.
mod rewrite is not what I want?


#4

You need to put that code at the top of your page, so that when you visit http://www.yourplaceforhomes.com/?state=wa&city=seattle, that code runs and returns a ‘Location’ header to the browser.

You will probably also have to add this line before the ‘Location’ one:

header(‘HTTP/1.0 302 Redirect’);

otherwise most browsers will ignore the Location header. Basically when you submit the form you’ll go to the horrible URL, which gets picked up by the PHP code and forwards you to a nice URL instead.