PHP Pretty URL两个mod_rewrite规则的组合?

时间:2016-08-11 06:44:26

标签: php apache .htaccess mod-rewrite xampp

我在根文件夹中有这个.htaccess代码来替换此网址:localhost/mysite/profile.php?username="someone"成为localhost/mysite/someone

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mysite/profile.php?username=$1

这项工作正常但我想要的是将子页面(例如页面)包含在localhost/mysite/about而不是localhost/mysite/about.php中。子页面也在根文件夹中。

我怎样才能实现这两次重写?

3 个答案:

答案 0 :(得分:2)

您可以在mysite/.htaccess中使用此代码:

RewriteEngine On 
RewriteBase /mysite/

# add .php internally to files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# handles profile URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [L,QSA]

答案 1 :(得分:0)

尝试下面的代码,它对我来说很好。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

答案 2 :(得分:0)

试试这个

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mysite/([A-Za-z0-9-]+)$ /mysite/profile.php?username=$1