View Full Version : htaccess issue

12-20-2008, 01:34 AM
First off - HELP!

I have looked high and low, up and down and still can't get this working. What I am trying to do it get a rewrite rule that handles up to 4 directories. Here is what I am looking for


This redirects to index.php?city=$1&state=$2&cat=$3&subcat=$4

But I won't always have 4, I want to be able to redirect to:

or /city/state/cat/subcat/

Here is my .htacess

RewriteEngine on
RewriteRule ^([A-Z,a-z,0-9,\_]*)/([A-Z,a-z,0-9,\_]*)/([A-Z,a-z,0-9,\_]*)/([A-Z,a-z,0-9,\_]*) /location.php?city=$1&state=$2&category=$3&subcat=$4

It works so long as I have the right number of slashes

For instance city/state/// works
or city/state/cat/

Is there some way to make this work without the slashes and be able the handle the 3 items above, or better yet, all four

Thanks in advance:D

12-20-2008, 03:54 PM
I'm not clued up on the .htaccess stuff, but this may help - url-rewriting (http://roshanbh.com.np/2008/03/url-rewriting-examples-htaccess.html)


12-20-2008, 04:10 PM
For instance city/state/// works
or city/state/cat/

I'd use a different method, like

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]
The above rule redirects everything in the requrest_uri to the index.php page (where 'page' is just a dummy variable).

Then I use
$req=split('/',$_SERVER['REQUEST_URI']); , which gives an array with all the items separated by / in the url. Thus I can interpret it as

$subcat=req[4];Based on this I'd write the conditions/includes to serve the corresponding pages (and a custom 404 page also, if required).