Chow Pak Hin Chow Pak Hin - 1 month ago 7
Apache Configuration Question

mod_rewrite: how to change the url except the homepage?

I would like to do the following:


  1. redirect
    /about
    to
    /about.php
    (hide the extension)

  2. redirect
    /(anything else)
    to
    /content.php?p=(anything else)

  3. while keeping the root
    http://domain.com
    to
    /index.php
    (without showing
    /index.php
    )



I tried this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !-d
RewriteCond %{REQUEST_URI} !-f
RewriteRule "^about$" about.php [NC,L]
RewriteRule ^((/+[A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ /_content.php?p=$1 [NC,L]


It succeeds to achieve the item 1 and 2, but this also rewrites the root
http://domain.com
to
http://domain.com/_content.php?p=
.

What have I done wrong? Thank you for your suggestions!

Answer

You can use these rules:

DirectoryIndex index.php
RewriteEngine On

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ content.php?p=$0 [QSA,L]