Chow Pak Hin Chow Pak Hin - 1 year ago 111
Apache Configuration Question

mod_rewrite: how to change the url except the homepage?

I would like to do the following:

  1. redirect
    (hide the extension)

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

  3. while keeping the root
    (without showing

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

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

Answer Source

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]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download