SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Whats the best way to implement mod_rewrite with PHP

    Hi everybody,
    I've finally decided to use mod_rewrite for SEO reasons. I am looking for advise from others that have used it. My situation is as follows, I have a listing page with a large number of products by different companies. To view the product details, I have a link, viewdetails?id=ererferfr4343444, where the ID is a random set of letters and numbers I created when inputting the information.

    How do I go about making this link more SEO friendly?


    Thanks in advance.

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    First of all, modify your viewdetails.php file to accept a more user-friendly name in the absense of an ID.

    So, for example, the request viewdetails.php?name=someCompany would return the same as viewdetails.php?id=t3ut249tun429tu (where that is the random ID of the same row containing the name someCompany).

    Then, in a .htaccess file:
    Code:
    RewriteEngine On
    RewriteRule ^details/([^/]+)/?$ viewdetails.php?name=$1
    Off Topic:

    Explaination of the Regular Expression:
    If the request starts with "details/", followed by 1 or more of any character apart from "/", with an optional "/" on the end.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply, i still dont really understand this, so, I should have my source link as something like
    Code:
    <a href="viewdetails.php?name=companyname_asdas34r34"? view details </a>

  4. #4
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No.

    That's just the first step. By implementing the mode rewrite, you'd be using:
    Code html:
    <a href="details/companyname">View Details</a>

    The reason you allow viewdetails.php to accept a name in the absense of an ID is so that the htaccess file can redirect to it.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #5
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But, the company name will have many products, so I need to have

    <a href="details/companyname_productname">View Details</a> ?

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ah, I see what you mean. In which case you could do
    Code:
    RewriteEngine On
    RewriteRule ^details/([^/]+)/?$ viewdetails.php?company=$1
    RewriteRule ^details/([^/]+)/([^/]+)/?$ viewdetails.php?company=$1&product=$2
    And the url would be:
    Code:
    details/companyname/productname
    For normality and the sake of keeping order to a hierarchy, I'd recommend the slash, not the underscore.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post

    For normality and the sake of keeping order to a hierarchy, I'd recommend the slash, not the underscore.
    Was just about to ask about this

    This makes sense now, thats for your help

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    That's what Sitepoint's here for

    Sorry about the confusion, I misread your original post.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh no, I just ran into an issue, when I do this, my CSS file for the viewdetails page does not load for some reason? When i view the page directly in the address bar its fine, but when using mod_rewrite its not loading ? Any ideas?

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yes. See, now the browser sees the page as in a subdirectory.

    What you should do is use an absolute URI rather than a relative one.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  11. #11
    SitePoint Addict
    Join Date
    Jul 2008
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cool, I got that, and I use GET to pull the variables or would it be better to grab the full address and then parse it to get my database key ?


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
  •