Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-20-2008, 12:34 AM   PM User | #1
webmmt
New to the CF scene

 
Join Date: Dec 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
webmmt is an unknown quantity at this point
Unhappy htaccess issue

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

domain.com/city/state/category/subcategory/

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:

/city/state/
/city/state/cat/
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
webmmt is offline   Reply With Quote
Old 12-20-2008, 02:54 PM   PM User | #2
student101
Regular Coder

 
student101's Avatar
 
Join Date: Nov 2007
Posts: 610
Thanks: 80
Thanked 13 Times in 13 Posts
student101 is on a distinguished road
Smile

I'm not clued up on the .htaccess stuff, but this may help - url-rewriting

Cheers
__________________
Thanks for your support!
Update MySQL with checkboxes | Tell A Friend | Delete MySQL with checkboxes

Give thanks & resolve when done :thumbsup:
student101 is offline   Reply With Quote
Old 12-20-2008, 03:10 PM   PM User | #3
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
Originally Posted by webmmt View Post
For instance city/state/// works
or city/state/cat/
I'd use a different method, like
Code:
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
PHP Code:
$req=split('/',$_SERVER['REQUEST_URI']); 
, which gives an array with all the items separated by / in the url. Thus I can interpret it as
PHP Code:
$city=req[1];
$state=req[2];
$cat=req[3];
$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).
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

Last edited by abduraooft; 12-20-2008 at 03:13 PM..
abduraooft is offline   Reply With Quote
Reply

Bookmarks

Tags
.htaccess, rewrite rule

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:12 PM.


Advertisement
Log in to turn off these ads.