SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    URL rewrite with htaccess

    HI
    i am beginner. someone built a website for me. it is written in php mySQL. the categories are retrieved and generated by the database. i would like to rewrite url for seo optimization. in my site there are 4 categories with url:

    www.mysite.com/products.php?cid=1
    www.mysite.com/products.php?cid=2
    www.mysite.com/products.php?cid=3
    www.mysite.com/products.php?cid=4

    i would like to make it show up like this:
    www.mysite.com/apple
    www.mysite.com/banana
    www.mysite.com/pear
    www.mysite.com/orange

    how can i do this, in a more simple way. is that simply changing .htaccess file, or is that related to rewrite mapping function. pls can you give me hint and exact example would be great. thanks

  2. #2
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    You know what regular expressions are?

    I can imagine two ways to do what you're asking, and I believe that using regular expressions in your .htaccess statements to redirect any URL like;
    www.mysite.com/products.php?cid=x (Where x is a variable) to www.mysite.com/x
    would be the easiest to manage as your website grows (because it'd be automated and you wouldn't need to keep updating your .htaccess file everytime you add a new category), but your keyword wouldn't appear in your new URL (unless you modified your variable names to be the keywords rather than numbers).


    BUT, another way would be to use a plain, old mod_rewrite like;
    # -301 Redirect-
    RewriteEngine on
    RewriteRule ^www\.mysite\.com?cid=1 www\.mysite\.com/apple
    but you'd have to keep adding new statements to your .htaccess for each new category. The plus would be your keyword would appear in the URL, which is the whole point if you're trying to improve SEO.


    I know I'm right in theory, but please let me know if my example code is correct, because it's been a long time since I've written any .htaccess statements and I'm trying to do it from memory.
    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

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

    Correct tool but bad code. I know you've been around the Apache forum before so you should know that a RewriteRule can only match the {REQUEST_URI}, not the {REQUEST_URI} - the ? delimiter - {QUERY_STRING}. Then, Redirects won't work because they won't match the query string, either.

    vvktse,

    That sort of code is reminiscent of some "canned programs" and messing about with those will mean that you've got to get into the module which generates the links and the module which converts the generated links into a database query. In "canned programs" like WordPress and ZenCart, it's not pretty (although there are now built-in functions in WP to do that for you).

    This IS a problem for mod_rewrite (if your coder "hand built" your website for you rather than used a "canned program") but you'd actually learn something if you checked out the tutorial article linked in my signature. If you don't understand something, you know where to find me.

    Finally, it's NOT a RewriteMap problem as that would require to you have access to the server's configuration file (or your vhosts.conf) which is just too dangerous for a noobie (no offense - you can bring the whole server down with the slightest typographical or formatting error).

    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

  4. #4
    SitePoint Member
    Join Date
    Aug 2012
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi thanks all for your advice. SOD, i tried your suggestion it is not working . i think it is the problem that the 4 categories page, /products.php?cid=1; /products.php?cid=2; /products.php?cid=3; /products.php?cid=4; all generated from the same page, simply pull the database from the mysql. so the rewrite does not work. i would be grateful if anyone can give me some hints and example for that. it is very difficult to find in the website, but i think it is not uncommon problem thought. and DK thanks, i will study your writing. is there any parts that you think is relevant so that i can make it mess up quickly. if you give me more hint or suggestion that would be great. should i rewritecond with the .htaccess file; and also have to write some script in the php main file?

  5. #5
    Keep Moving Forward gold trophysilver trophybronze trophy
    Shaun(OfTheDead)'s Avatar
    Join Date
    Nov 2005
    Location
    Trinidad
    Posts
    3,746
    Mentioned
    45 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn
    Correct tool but bad code.
    argh! Darn, I knew it!

    Quote Originally Posted by vvktse
    hi thanks all for your advice. SOD, i tried your suggestion it is not working .
    Man, listen to DK Lynn from now on; He's brilliant.

    Trying to fill the unforgiving minute
    with sixty seconds' worth of distance run.

    Update on Sitepoint's Migration to Discourse

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

    Well, that's a first! Thanks for the compliment!

    Others, I received a PM from vvk and responded that way. HOWEVER, I believe that it's important for a webmaster to KNOW what his code is doing and continue to recommend a visit to my signature's tutorial. It is long (especially with all the sample coding problems) but it HAS been very helpful to members over the years.

    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


Tags for this Thread

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
  •