除了一个文件夹外,mod_rewrite删除文件夹+扩展名

时间:2014-06-20 03:30:11

标签: regex apache .htaccess mod-rewrite

我的mod_rewrite非常好奇。我有以下.htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ErrorDocument 404 /404.php

RewriteRule ^approval/(.*)/(.*)/[0-9]$ /view/approval/$1/$2\.php [NC,N]
RewriteRule ^approval/(.*)/(.*)$ /view/approval/$1\.php?id=$2 [NC,N]
RewriteRule ^approval/(.*)$ /view/approval/$1\.php [NC]

RewriteRule ^dashboard/(.*)/(.*)/[0-9]$ /view/dashboard/$1/$2\.php [NC,N]
RewriteRule ^dashboard/(.*)/(.*)$ /view/dashboard/$1\.php?id=$2 [NC,N]
RewriteRule ^dashboard/(.*)$ /view/dashboard/$1\.php [NC]

批准工作的重写规则 - 他们删除了“view”文件夹和.php(因此/view/approval/file/thisfile.php变为/approval/file/thisfile)。

但是,仪表板的相同确切代码和结构不仅不起作用,而且还倾向于添加.php。因此/view/dashboard/file/managethis.php变为/view/dashboard/file/managethis\/.php/(正在逃避斜线)

所有网址都位于名为“view”的单独文件夹中,并且所有网址都已删除.php。如何在一个RewriteRule中执行此操作,而不是必须重写每个文件夹?

1 个答案:

答案 0 :(得分:1)

而不是.*在你的正则表达式中使用[^/]+也不需要在右侧转义点:

ErrorDocument 404 /404.php

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule  - [L]

RewriteRule ^dashboard/([^/]+)/([^/]+)/[0-9]/?$ /view/dashboard/$1/$2.php [NC,L,QSA]
RewriteRule ^dashboard/([^/]+)/([^/]+)/?$ /view/dashboard/$1.php?id=$2 [NC,L,QSA]
RewriteRule ^dashboard/([^/]+)/?$ /view/dashboard/$1.php [NC,L]