doudao1922 2013-05-28 14:18
浏览 21
已采纳

.htaccess url重写

This is my .htaccess. It redirects /contact to index.php?page=contact
This works perfectly. But with this .htaccess, if someone puts /contact.php, they'll get an error.

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/?$ index.php?page=$1  [QSA]

Basically, is there any way to only rewrite if there's no .php in the URL?
If there's a .php in the URL, e.g. contact.php just load the normal .php page.

How could I do this?

  • 写回答

1条回答 默认 最新

  • doulechou0700 2013-05-28 14:29
    关注

    Add something like this before the RewriteRule:

    RewriteCond %{REQUEST_FILENAME} !-f
    

    -f means "is a file". With this rule you are saying "requested file name is not an existing file" (in the DocumentRoot, ofc).

    More info and options: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewritecond

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?