Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3

Thread: htaccess issue

  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  2. #2
    Regular Coder
    Join Date
    Nov 2007
    Posts
    634
    Thanks
    80
    Thanked 15 Times in 15 Posts

    Smile

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

    Cheers

  3. #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    15,016
    Thanks
    164
    Thanked 2,246 Times in 2,233 Posts
    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).
    Last edited by abduraooft; 12-20-2008 at 03:13 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •