我的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中执行此操作,而不是必须重写每个文件夹?
答案 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]