Mod URL with htaccess?

Discussion in 'Web Design & Programming' started by Gregg, May 20, 2011.

  1. Gregg

    Gregg Geek Trainee

    Likes Received:
    0
    Trophy Points:
    1
    Hello all,

    I'm having a great difficulty understanding how to use the url rewrite capability of htaccess.. this is what I would like to do:

    Convert the user typed url: www.something.com/about/
    Into: www.something.com/index.php?p=about

    With much thanks, could anybody tell me how I could do this?

    Also, while we're discussing htaccess, I am wondering if its possible to redirect any 404 error..or any error for that matter to the homepage with errors.. (www.something.com/index.php?p=errors)

    Thanks guys, you're great!
     
  2. Sniper

    Sniper Administrator Staff Member

    Likes Received:
    59
    Trophy Points:
    63
    .htaccess rules can be tricky, thats why I prefer do it how wordpress does it.

    .htaccess
    Code:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    then in your index.php

    PHP:
    <?php

    echo '<pre>';
    print_r($_SERVER);
    echo 
    '</pre>';

    $path trim($_SERVER['REQUEST_URI'], '/');

    if (
    $path == 'about')
    {
      echo  
    'about page';
      
    //or include('about.php');
      //or go into the database to get the page contents
    }
     

Share This Page