[newbie] mod_rewrite - Htaccess - remove numbers showing in my URL

Discussion in 'Apache' started by pagaling, Apr 27, 2011.

  1. #1
    Hi Guys,

    I'm fairly new to editing .htaccess, I would like to ask a little help please.

    My url shows like this:

    DOMAIN/11-for-sale-by-owner/1/listings.html

    I was hoping someone could help me show the url like this:

    DOMAIN/for-sale-by-owner

    Another example is this:

    DOMAIN/10-john-doe/store.html

    I was hoping it can be changed to this:

    DOMAIN/john-doe or DOMAIN/johndoe

    Basically, I want the numbers removed and also the html name at the end


    Both values are unique, for sale by owner is a category while john doe is a user/member


    Here is my .htaccess

    ## Uncomment this if FollowSymLinks is not already enabled on your server
    Options +FollowSymLinks

    ## Use mod rewrite
    ## Comment the line below if RewriteEngine is already enabled on your server
    RewriteEngine On

    ## Uncomment the line below and change the path if your script is installed
    ## in a different path than the root folder of your domain

    #RewriteBase /

    RewriteRule ^index.html index.php [nc]
    RewriteRule ^recent_ads.html recent_ads.php [nc]
    RewriteRule ^register.html register.php [nc]
    RewriteRule ^pre-register.html pre-register.php [nc]
    RewriteRule ^login.html login.php [nc]
    RewriteRule ^logout.html logout.php [nc]
    RewriteRule ^favourites.html favourites.php [nc]
    RewriteRule ^contact.html contact.php [nc]
    #RewriteRule ^advanced_search.html advanced_search.php [nc]
    RewriteRule ^listings.html listings.php [nc]
    RewriteRule ^pre-submit.html pre-submit.php [nc]
    RewriteRule ^([0-9]+)-([^\/]+)?/content.html$ content.php?id=$1 [nc,qsa]
    RewriteRule ^([0-9]+)-([^\/]+)/index.html$ index.php?category=$1 [nc,qsa]
    RewriteRule ^([0-9]+)-([^\/]+)/details.html$ details.php?id=$1 [nc,qsa]
    RewriteRule ^([^\/]+)/([^\/]+)/([^\/]+)/recent_ads.html$ recent_ads.php?page=$1&order=$2&order_way=$3 [nc,qsa]
    RewriteRule ^([0-9]+)-([^\/]+)/user_listings.html$ user_listings.php?id=$1 [nc,qsa]
    RewriteRule ^([0-9]+)-([^\/]+)/([^\/]+)/([^\/]+)/([^\/]+)/user_listings.html$ user_listings.php?id=$1&page=$3&order=$4&order_way=$5 [nc,qsa]
    RewriteRule ^([^\/]+)/([^\/]+)/([^\/]+)/favourites.html$ favourites.php?page=$1&order=$2&order_way=$3 [nc,qsa]
    RewriteRule ^([0-9]+)-([^\/]+)/store.html$ store.php?id=$1 [nc,qsa]
    RewriteRule ^([0-9]+)-([^\/]+)/([^\/]+)/([^\/]+)/([^\/]+)/store.html$ store.php?id=$1&page=$3&order=$4&order_way=$5 [nc,qsa]

    RewriteCond %{REQUEST_URI} ^(.*)(/listings.html)(.*)$ [NC]
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule (.*) ./listings.php?$1


    Which part of the code should I delete or modify? Should I add something?

    I'd like to thank anyone in advance. God Bless.
     
    pagaling, Apr 27, 2011 IP
  2. lkraj

    lkraj Greenhorn

    Messages:
    72
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #2
    There are many ways, you can do this:
    You can change DOMAIN/10-john-doe/store.html to
    DOMAIN/store-john-doe or
    DOMAIN/store/johndoe or
    DOMAIN/store/john-doe

    Change the following rewrite rule accordingly
    RewriteRule ^([0-9]+)-([^\/]+)/store.html$ store.php?id=$1 [nc,qsa]

    And in store.php, maintain a list, recognize storeName john-doe instead of the id: 10. You might have to use URL-encoding, depending on how the store names are.
     
    lkraj, Apr 27, 2011 IP
  3. pagaling

    pagaling Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks lkraj. You just made my day. I posted this same question in DevShed and a Top3 Mod named Requinix just insulted me:

    My simple serious question was:
    Can I remove ([0-9]+)? Will that take care of the number problem? Thanks again

    He answered:
    Try it. Spoiler: no, it won't.

    That's just basically how they answer in their forum IMHO useless answers. I'm happy I got to your forum. Now at least I have an idea what file to check (store.php) although honestly I do not know how to change the rewrite rule. I would really appreciate if you can point me again to the right direction.

    Thanks again.
     
    pagaling, Apr 27, 2011 IP
  4. lkraj

    lkraj Greenhorn

    Messages:
    72
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #4
    If you are planning to go ahead with DOMAIN/store/johndoe, there are 3 things you have to do:

    1. On your website where-ever you have links for the format- DOMAIN/10-john-doe/store.html, you need to change them to format- DOMAIN/store/johndoe

    2. You can try the following rewrite rule
    Change ^([0-9]+)-([^\/]+)/store.html$ store.php?id=$1
    to
    ^store/(.*)$ store.php?store=$1
    You can google for mod_rewrite and regular expressions to get an idea on what they mean and what else you can do

    3. Store.php is currently expecting 'id' parameter, instead you need to change the code to look for 'store' parameter and you also need to maintain the list of store-names so that store.php can pick the right content and give it back to the user.

    4. You need to do similar 1-3 steps for listings (for-sale-by-owner) as well.

    Before you spend time doing all the above changes and testing them, analyze if this is worth the time and if it does really matter if the URL has numbers.
     
    lkraj, Apr 28, 2011 IP
  5. pagaling

    pagaling Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thank you so much. I'll be reading some more and analyze if this will be worth doing. But right now I'm sure I learned from you. Thanks again.
     
    pagaling, Apr 28, 2011 IP