.htaccess重写两个或多个PHP文件的规则

时间:2014-03-18 07:38:59

标签: php apache .htaccess mod-rewrite url-rewriting

现在我有.htaccess文件为我的profile.php(根目录)重写url

    RewriteEngine On
    RewriteCond %(REQUEST_FILENAME) !-f
    RewriteCond %(REQUEST_FILENAME) !-d
    RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1

此代码将http://127.0.0.1/projects/webproject/profile.php?username=sarah转换为http://127.0.0.1/projects/webproject/sarah

情况: 说,我有“update_availability.php”(根目录),并使用此格式http://127.0.0.1/projects/webproject/update_availability.php?month=03&day=18&year=2014访问它。

我想将网址更改为http://127.0.0.1/projects/webproject/update_availability.php/03/18/2014

我想知道的事情:

  1. 是否可以有一个.htaccess文件(根目录)和其他php文件的重写规则?
  2. 如何将http://127.0.0.1/projects/webproject/update_availability.php?month=03&day=18&year=2014 转换为 http://127.0.0.1/projects/webproject/update_availability.php/03/18/2014
  3. 我是.htaccess的新手。我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:1)

您可以使用:

RewriteEngine On
RewriteBase /webproject/

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

RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^(update_availability(?:\.php)?)/([0-9]+)/([0-9]+)/([0-9]+)/?$ $1?month=$2&day=$3&year=$4 [L,QSA,NC]