简体   繁体   中英

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!

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM