How to use SEO-friendly URLs with .htaccess ?

Discussion in 'PHP' started by gingerbreadweb, Nov 2, 2009.

  1. #1
    Hey all

    I want to use SEO-friendly URLs on certain pages of a website. What I want to achieve is the same thing you can do with WordPress, in other words the title of the blog post becomes the filename.

    For example...

    Original URL:
    www.website.com/page.php?id=54
    Blog post title is "My first blog post"

    New URL:
    www.website.com/my-first-blog-post
    OR
    www.website.com/my-first-blog-post.php

    Does anyone know how to do this?


    Alternatively, is it possible to use .htaccess to change the URL to match the page title, retrieved from the <title> tag in the HTML of the page? I don't know if this is easier or more difficult...

    Thanks for your time
     
    gingerbreadweb, Nov 2, 2009 IP
  2. xenon2010

    xenon2010 Peon

    Messages:
    237
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think these kind of links generated using PHP it self..
    for example. you need to generate a whole new page with php using the original php script..
     
    xenon2010, Nov 2, 2009 IP
  3. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #3
    For links like /post/my-post-title.php
    .htaccess
    
    RewriteEngine On
    RewriteBase /post/
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.+?).php$	/post.php?url=$1 [QSA]
    
    
    Code (markup):
    You'll have to make a column in databse where you store post urls.
    And then search post by this value like:
    
    $q = "SELECT * FROM `posts` WHERE url LIKE '".$_GET['url']."'";
    
    PHP:
    Don't forget to sanitize $_GET['url'];
     
    AsHinE, Nov 2, 2009 IP
  4. gingerbreadweb

    gingerbreadweb Active Member

    Messages:
    512
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #4
    Thanks guys, will give this a try :)
     
    gingerbreadweb, Nov 3, 2009 IP