regex - .htaccess mod_rewrite 具有 3 个目录级别
问题描述
如何在 Htaccess 上管理 3 个子目录级别?
解释:
http://example.com/category1/
http://example.com/category2/
http://example.com/category3/
RewriteRule to *category.php?category=$1*
http://example.com/category1/type1/
http://example.com/category2/type7/
http://example.com/category3/type8/
RewriteRule to *type.php?category=$1&type=$2*
http://example.com/category1/type1/company3/
http://example.com/category2/type7/company4/
http://example.com/category3/type8/company9/
RewriteRule to *company.php?category=$1&type=$2&company=$3*
.htaccess 文件位于初始根文件夹(其中 index.php)
下一个是最好的方法吗?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)(?:/)?$ /category.php?category=$1 [L]
RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /type.php?category=$1&type=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)(?:/.*)?$ /company.php?category=$1&type=$2&company=$3 [L]
解决方案
你很接近。请注意,RewriteCond
仅适用于下一个立即RewriteRule
数,因此您的检查!-f
将仅用于第一条规则。您可以有一个单独的规则来丢弃所有针对现有文件和目录的请求。
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)/?$ category.php?category=$1 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ type.php?category=$1&type=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ company.php?category=$1&type=$2&company=$3 [L,QSA]
推荐阅读
- azure - 无法从 Azure 逻辑应用连接到 Azure DevOps
- python - 从相似(但略有不同)的信息中得出不同的观点?
- python - ModuleNotFoundError:没有名为“book”django的模块
- python - 无法在 seaborn 热图动画中正确标记刻度线
- c++ - 如何使用 Eclipse(4.20.0 版本)和 Matlab(R2021a)在 Windows 中编译 mex 函数
- php - MySQL 错误:无法添加外键约束(PrestaShop 模块)
- c# - Auto/ManualResetEvent 是否同步内存访问?
- javascript - 如何在反应中为嵌套的对象数组设置状态?
- python - 当 start 尚未定义时 play_again 是如何工作的?我不能让它在“fällan rum”工作
- python-3.x - 仅计算一个主题的连贯性