htaccess删除两个不同页面中的扩展名

时间:2014-01-05 10:36:40

标签: php regex apache .htaccess mod-rewrite

我使用htacess删除php扩展并且它工作但我也有一个名为profile的页面,我缩短但是使页面名称被视为个人资料用户名,有没有办法解决这个问题?

我想要的结果是这样的:

localhost/path_folder/index.phplocalhost/path_folder/index

localhost/path_folder/profile?username=namelocalhost/path_folder/name

和删除扩展程序的htaccess代码为:

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

,个人资料页面的代码为:

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

2 个答案:

答案 0 :(得分:1)

将此代码放入DOCUMENT_ROOT/.htaccess文件中:

RewriteEngine On

## First try To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /path_folder/$1.php [L]

## if PHP file not found then load profile page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /path_folder/profile.php?username=$1 [L,QSA]

答案 1 :(得分:0)

如果要删除文件扩展名

RewriteEngine On 
RewriteRule ^path_folder/([^/]*)$ path_folder/$1.php [L]

缩短userprofiles的网址

RewriteEngine On
RewriteRule ^path_folder/profile/([^/]*)$ path_folder/profile?username=$1 [L]