SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .htaccess and Mod Rewrite URLS?

    Hi Everyone,

    I'm trying to figure out how to rewrite a URL. I've looked at a few sitepoint topics about this subject matter but I'm having a hard time trying to figure it out on my own.

    I want to take the below URL
    http://whatsmyowncarworth.com/state....rhode%20island

    and

    make it look like this
    http://whatsmyowncarworth.com/rhode-island

    I've written the below code in my .htaccess but it's still not working. Any help would be appreciated.

    I've tried all of the below syntax once but nothing is working. What am I doing wrong? Thanks everyone!

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*)/(.*)/$ /state.php?param1=$1
    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*)/(.*)/$ state.php?param1=$1
    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*)/(.*)/$ state.php?state=$1
    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*)/(.*)/$ /state.php?state=$1

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    scm,

    I cover just this topic in developing the regex portion of my signature's tutorial on mod_rewrite. Please come back with questions.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi DK,

    Thanks for the email and tutorial.

    I read through your website and I'm trying the below syntax in my htaccess. I thought I was doing it correctly but I'm getting internal server errors on my website.

    http://whatsmyowncarworth.com/new-york
    http://whatsmyowncarworth.com/massachusetts
    http://whatsmyowncarworth.com/nevada
    http://whatsmyowncarworth.com/test.html

    My .htaccess code
    Code:
    RewriteEngine on
    RewriteRule ^test\.html$ test.php [L]
    RewriteRule ^([a-zA-Z_]+)$
    state.php?state=$1 [L]

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    scm,

    Quote Originally Posted by scm22ri View Post
    First, the tutorial should have corrected your description as you want mod_rewrite to accept a state and redirect it to state.php with the value of the state in the URI assigned to the state key.

    Note: You can redirect to the preferred (non-servable) URI then back but that's more of an advanced topic. Best to get it right before delving into that, IMHO.

    Second, the tutorial described single name states (no spaces) as well as the changes required when there is a space in the state's name (i.e., best to change the link and replace spaces with _'s because %20 is so !@#$ ugly). Also, if you don't want to accept "garbage" as a state name, you really need to create a list of acceptable states (also shown in the tutorial).

    Third, your original attempt shows a space between rhode and island but you've shown a hyphen in the link you wanted to use.

    Okay, comments on your original code above:

    Code:
    Options +FollowSymLinks # should already be in the httpd.conf so this is not (normally) necessary
    RewriteEngine on
    RewriteRule (.*)/(.*)/$ /state.php?param1=$1
    Is $2 a "throw-away"? If so, then why create that variable?

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*)/(.*)/$ state.php?param1=$1
    How is this any different? If you're already in the DocumentRoot (where I like to keep my mod_rewrite code), there is no difference at all.

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*)/(.*)/$ state.php?state=$1
    Okay, uses state as a key rather than param1.

    Code:
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule (.*)/(.*)/$ /state.php?state=$1
    Ditto.

    http://whatsmyowncarworth.com/new-york
    http://whatsmyowncarworth.com/massachusetts
    http://whatsmyowncarworth.com/nevada
    http://whatsmyowncarworth.com/test.html

    These are your test URIs? What does test.html have to do with your state problem?

    Code:
    RewriteEngine on
    RewriteRule ^test\.html$ test.php [L] # irrelevant to state problem
    
    
    RewriteRule ^([a-zA-Z_]+)$
    state.php?state=$1 [L]
    THIS is what is causing the 500's as there should be no line return after the regex. 500's are almost always the result of syntax errors in code (or the module not being available).

    Changing to the one-liner:

    Code:
    RewriteRule ^([a-zA-Z_]+)$ state.php?state=$1 [L]
    this code WILL work for your second and third test URIs (and the fourth) but not new-york. Note that you've included my preferred _ while the link used a - instead. When you use regex, you should be very specific with your directives. Unfortunately, this will also match "garbage", "nEW_yORK" and a lot of other nonsense. Had you been specific about what your database query is expecting to find, use of

    Code:
    RewriteRule ^(new-york|massachusetts|nevada)$ state.php?state=$1 [L]
    WOULD have worked. However, when testing, change your [L] flag to [R=301,L] so you can see the redirection in the URI (and KNOW that your mod_rewrite is working properly) - you can remove the R=301, when you're ready to go live on your website.

    Please have a re-read of the Regex section of the tutorial.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey DK,

    I think I'm understanding this a bit better. I'm fairly confident I wrote this part of my .htaccess correctly but for some reason css of my website isn't displaying.

    http://whatsmyowncarworth.com/massachusetts/boston

    http://whatsmyowncarworth.com/city-b...ts&city=boston

    The both pages above are the exact something but the second URL shows my CSS and the first one does not. Maybe it's my .htaccess code?

    Code:
    RewriteEngine On
    RewriteRule ^([a-zA-Z_]+)$ state.php?state=$1 [L]
    RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)$ city-breakdown.php?state=$1&city=$2 [L]

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    scm,

    Success! Your mod_rewrite is working.

    The "missing support files" are addressed in my signature's tutorial, too. Remember, mod_rewrite is changing the directory level but not announcing that change to your visitors so you need to do that either via the <base> tag or via absolute links.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey DK,

    I'm glad it's working. Thanks for your help but now my goal is to figure out my hyphen- issue. I want to prepend a hyphen to every state (and also city but now I'll focus on state) on my website. I'm fairly certain I've done that correctly but for some reason if you click on a city none of my data is showing up. I don't believe my .htaccess is wrong? What do you think? Thanks again.

    // I'm prepending the - right before the a
    Code:
    RewriteEngine On
    RewriteRule ^([-a-zA-Z_]+)$ state.php?state=$1 [L]
    RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)$ city-breakdown.php?state=$1&city=$2 [L]
    This is my vehicle-display.php syntax
    http://whatsmyowncarworth.com/vehicle-display.php
    PHP Code:
    while($row mysql_fetch_array($sql)){

    $state $row["state"];

       
    $state_up first_cap($state);

    echo 
    "$state".'<br>';

       
    $state lowercase($state);
       
    $state str_replace (' ''-'$state );

    echo 
    "<a href ='http://whatsmyowncarworth.com/$state'>$state_up Inventory</a>".'<br>';
    echo 
    '<hr/>';



  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    scm,

    Tip: Before I start coding, I define EXACTLY what links will look like and what those links will map to. IMHO, that should eliminate your "hyphen-issue."

    Redirection: state-city => city-breakdown.php?state=state&city=city.

    That's fine as long as you can define state and city precisely, i.e., WITHOUT hyphens (which are used to separate city from state - see why I prefer _'s yet?).

    WHY prepend your URI values for state/city with -'s? First, that complicates your mod_rewrite code (slightly) but it looks ugly.

    [as]Also, use PHP variables freely in your PHP code rather than bounding back and forth with first/lower or " " to "-".[/as]

    Code:

    Code:
    RewriteEngine On
    RewriteRule ^([-a-zA-Z_]+)$ state.php?state=$1 [L]
    RewriteRule ^([a-zA-Z_]+)/([a-zA-Z_]+)$ city-breakdown.php?state=$1&city=$2 [L]
    I'd move the state's - outside and before the parenthetical as it's not part of your state's name (again, my reason for _'s to replace spaces ... which you've already accounted for in your mod_rewrite code but NOT your PHP code!?!). Unfortunately, you're not consistent with your city-breakdown code which will NOT match any hyphen in your URI.

    Ergo, back to the planning stage. After all, PPPPPPP (Proper Prior Planning Prevents Piss Poor Performance) and, once you have the definition (which I use to find the "kinks" in the code), it's easy to turn it into pseudo code and then into mod_rewrite code.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey dklynn,

    I figured out my htaccess!

    http://whatsmyowncarworth.com/rhode_island
    http://whatsmyowncarworth.com/rhode_...ast_greenwhich
    http://whatsmyowncarworth.com/rhode_...rth_providence

    For anyone that's curious here's my .htaccess

    Code:
    RewriteEngine On
    RewriteRule ^([_a-zA-Z_]+)$ state.php?state=$1 [L]
    RewriteRule ^([_a-zA-Z_]+)/([_a-zA-Z_]+)$ city-breakdown.php?state=$1&city=$2 [L]
    
    ErrorDocument 404 /cool404.php
    I have one remaining question.

    The below URL
    http://whatsmyowncarworth.com/state....rhode%20island

    is the same page as this below URL
    http://whatsmyowncarworth.com/rhode_island

    Is there anyway to tell google, yahoo and bing to just index the second URL? -> (http://whatsmyowncarworth.com/rhode_island)

    Thanks

  10. #10
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,607
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    scm,

    Check your character range definitions as I don't believe that you need TWO _'s in each one. If you're using hyphens, change the first one each time so Apache doesn't get confused.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

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