多个ModRewrite - .htaccess

时间:2013-04-04 01:33:28

标签: php .htaccess mod-rewrite

我正在尝试进行两次mod重写。让我解释一下。


  - 我需要一个重写,如果没有文件或目录存在,重定向到用户页面。 | 我已经做到了。   - 接下来,我想从 .php 文件中删除扩展程序。

我已经完成了第一次,我只是想不通怎么做第二次?


我的代码已经:

RewriteEngine On


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.*)$ users.php?name=$1 [QSA,L]

所以我需要的是,我只需要删除.php扩展名并保持当前的重写工作。

2 个答案:

答案 0 :(得分:1)

这应该是你完整的.htaccess:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

## redirect to a userpage
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ users.php?name=$1 [QSA,L]

答案 1 :(得分:0)

删除.php扩展名:

RewriteEngine On
# turn on the mod_rewrite engine

RewriteCond %{REQUEST_FILENAME}.php -f
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_URI} !/$
# AND the request is not for a directory
RewriteRule (.*) $1\.php [L]
# redirect to the php script with the requested filename

请指出:

http://www.mysite.com/index