Rewrite URL using .htaccess or others

Discussion in 'Search Engine Optimization' started by sonu12345, Apr 23, 2009.

  1. #1
    I have a problem in Rewrite URL.
    I have hyperlink & URL like 172.16.2.44/restaurant/home.php?state_id=3&city_id=7
    here state_id & city_id are not fixed.
    now i want this URL like 172.16.2.44/restaurant/home.php/3/7
     
    sonu12345, Apr 23, 2009 IP
  2. vansterdam

    vansterdam Notable Member

    Messages:
    3,145
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    245
    #2
    This is actually a quite common use of the .htaccess modrewrite. Let me quick find a good tutorial that explains this....here's one: http://www.tutorio.com/tutorial/search-engine-friendly-urls-with-mod-rewrite

    You will want to use code like this:

    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^restaurant/home.php/([0-9]+)/([0-9A-Za-z]+)/ restaurant/home.php?state_id=$1&city_id=$2

    You may need to modify that slightly, but just check some other modrewrite tutorials.
     
    vansterdam, Apr 23, 2009 IP
  3. dataflurrydotcom

    dataflurrydotcom Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    dataflurrydotcom, Apr 23, 2009 IP
  4. jitendraag

    jitendraag Notable Member

    Messages:
    3,982
    Likes Received:
    324
    Best Answers:
    1
    Trophy Points:
    270
    #4
    I think the ([0-9A-Za-z]+) should be ([0-9]) because it's city id.
     
    jitendraag, Apr 23, 2009 IP
  5. newlogo

    newlogo Peon

    Messages:
    3,931
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thourgh programming also u can create functionality in back end with old url, new url and update button
     
    newlogo, Apr 23, 2009 IP
  6. Canonical

    Canonical Well-Known Member

    Messages:
    2,223
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    110
    #6
    You need to be careful when you start rewriting URLs like this. How you implement it might depend on whether it is 1) a brand new site where you've NEVER exposed the non-SEO friendly URLs (like 172.16.2.44/restaurant/home.php?state_id=3&city_id=7) or 2) an old site that previously used non-SEO friendly urls (like 172.16.2.44/restaurant/home.php?state_id=3&city_id=7) that you are now converting to SEO friendly URLs. The solutions for these two scenarios are usually different.

    A brand new site where you've NEVER exposed the non-SEO friendly URLs
    This is the easier of the two situations described above to deal with. Since no one has ever seen the non-SEO friendly URLs you'll be rewriting to, you really only have to implement URL rewriting from the SEO friendly URL (172.16.2.44/restaurant/home.php/3/7) to the non-SEO friendly URLs (172.16.2.44/restaurant/home.php?state_id=3&city_id=7).

    I think vansterdam is close... not sure why there is A-Za-z in the $2 pattern match though... and I would allow an optional trailing '/' since the last part of the URI does not have a file extension (Others might interpret it as a folder name and include a trailing '/' when linking to it).

    So I would guess the following might be a little more correct (but I haven't tested it so...):

    Hopefully the example above is NOT your real life problem you're trying to solve and is only an example you threw out. I really don't see either of the above as being SEO friendly URLs. I don't think going from 172.16.2.44/restaurant/home.php?state_id=3&city_id=7 to 172.16.2.44/restaurant/home.php/3/7 adds any SEO benefit at all. I personally don't care for filenames in the middle of my URL either like 172.16.2.44/restaurant/home.php/3/7. But that is just me.

    I think the following would be MUCH more SEO friendly:

    172.16.2.44/restaurant/new-york/ithica/
    or
    172.16.2.44/restaurant/ny/ithica/

    You can use the RewriteMap directive in Mod Rewrite to lookup the number that equates to a state name or abbreviation as well as the number that equates to the city. The lookup can be done by referencing a text map file or by calling an executable to, say, lookup the cross-reference from a database. These are very useful for situations where you want to map a text value (like "new-york" or "ny") to a totally different value (like 3).


    Converting an old site that previously used non-SEO friendly URLs to using SEO friendly URLs:
    This presents a bit more of a challenge since your non-SEO friendly URLs are out there in the wild. In these situations you need to implement a 301 redirect AND a URL rewrite for every URL.

    If other sites are already linking to you with URLs like 172.16.2.44/restaurant/home.php?state_id=3&city_id=7 then you will want to implement a 301 redirect from 172.16.2.44/restaurant/home.php?state_id=3&city_id=7 to your SEO friendly URL like 172.16.2.44/restaurant/ny/ithica/. Then when the browser requests 172.16.2.44/restaurant/ny/ithica/ you will want to URL rewrite it back to 172.16.2.44/restaurant/home.php?state_id=3&city_id=7.

    This will get the non-SEO friendly URL out of Google's index and give credit to 172.16.2.44/restaurant/ny/ithica/ for all inbound links to 172.16.2.44/restaurant/home.php?state_id=3&city_id=7.

    When you create the rewrite rule and conditions to 301 from 172.16.2.44/restaurant/home.php?state_id=3&city_id=7 to 172.16.2.44/restaurant/ny/ithica/ I would not assume that the query string parameters state_id and city_id will always appear in the same order. More than likely, the following two query strings will return the same content:

    172.16.2.44/restaurant/home.php?state_id=3&city_id=7
    172.16.2.44/restaurant/home.php?city_id=7&state_id=3

    So build your rules appropriately.
     
    Canonical, Apr 24, 2009 IP
  7. sonu12345

    sonu12345 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I simply write this code..
    Options +FollowSymLinks
    RewriteEngine On
    RewriteRule ^restaurant/home.php/([0-9]+)/([0-9A-Za-z]+)/ restaurant/home.php?state_id=$1&city_id=$2

    But it says Server Error.
    My question any other document i have to write here.
     
    sonu12345, May 4, 2009 IP
  8. sonu12345

    sonu12345 Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I write this .htaccess file. But its not working.
    Options -Indexes
    Options +FollowSymLinks
    <IfModule mod_rewrite.c>
    <IfModule mod_dir.c>
    DirectoryIndex index.php index.html index.htm default.htm default.html default.asp /mtview.php
    </IfModule>
    RewriteEngine on
    RewriteRule ^restaurant/home.php/([0-9]+)/([0-9A-Za-z]+)/ restaurant/home.php?state_id=$1&city_id=$2
    </IfModule>
     
    sonu12345, May 4, 2009 IP
  9. jitendraag

    jitendraag Notable Member

    Messages:
    3,982
    Likes Received:
    324
    Best Answers:
    1
    Trophy Points:
    270
    #9
    Some hosting providers would expect you to define a RewriteBase

    Try adding this just after RewriteEngine on; RewriteBase /
     
    jitendraag, May 4, 2009 IP