views:

437

answers:

2

I want to be able to rewrite a URL from:

// examples

http://example.com/location/New York, NY  -->
http://example.com/location/index.html?location=New York, NY

http://example.com/location/90210  -->
http://example.com/location/index.html?location=90210

http://example.com/location/Texas -->
http://example.com/location/index.html?location=Texas

http://example.com/location/ANYTHING....  -->  
http://example.com/location/index.html?location=ANYTHING...

using .htaccess and mod_rewrite.

Anyone know how to do this?

I have tried:

RewriteEngine on
RewriteCond %{REQUEST_URI} !location/index.html
RewriteRule ^location/(.*)$ /location/index.html?location=$1

However, it is not passing the GET location variable to the /location/index.html page when you use the "pretty url" (e.g. http://example.com/location/90210).

I know this b/c when I echo out to the screen (using javascript) the location GET variable when the long url is used, it's set but when the pretty (short) url is used, the location GET variable is undefined.

Thanks in advance

+2  A: 

Your last example should work; I'd also check the condition to be case-insensitive (to avoid /LoCation/indeX.htmL from being parsed), terminate rewrite with [L] (to prevent infinite loops) and add QSA (for appending queries):

RewriteEngine on
RewriteCond %{REQUEST_URI} !location/index.html [NC]
RewriteRule ^location/(.*)$ /location/index.html?location=$1 [L,QSA]

How do you read out (and echo) the location GET variable? "I'm using JavaScript to echo out an alert that prints the "location" variable."

JavaScript runs inside your browser ("client-side"), therefore it works with the same data that your browser sees. I.e., if you point your browser at http://www.example.com/foo/bar/ , then no matter what rewriting you use at the server, Javascript will still see "http://www.example.com/foo/bar/" as the location.

To access the GET variables, you need some code to access them when the page is generated ("server-side"), before it is sent to the browser. For example, when you have a PHP-capable server, the following script at http://www.example.com/location/index.php and you redirect to it through something like the above code, it will be able to access and work with the GET variables:

<?php
echo 'The location you entered is ' . $_GET['location'] . '.';
?>

When combined with the rewrite, for URL http://www.example.com/location/Houston,TX it will print out this:

The location you entered is Austin,TX.

(of course, there are many server-side languages, I'm using PHP as an example I'm most familiar with)

Piskvor
just a note @timmy_ i tested this as well and it works fine. you may want to double check your rewrite log if you can set one up to see if there's something specific to your config that's preventing this from working
Owen
I'm using JavaScript to echo out an alert that prints the "location" variable.Owen, where do I find the rewrite log?
Timmy_
unfortunately it has to be set i the server config/virtual host. if you have access to it, look at: RewriteLog and RewriteLogLevel to set up a rewrite log
Owen
A: 

Just to reiterate, the solution posted by Piskvor does work as expected. As per the comments on that, you're using javascript to pick up the query string, which is the problem. As far as javascript is concerned, the original URL is the one it sees. You can confirm this for yourself quickly:

alert(document.location.href);

if you need to get the value in javascript, i'd suggest using something like:

var regex = /location\/(.*)$/;
var query = document.location.href.match(regex);
alert(query[1]);

// query[1] will contain "90210" in your example
// http://example.com/location/90210
Owen