[英].htaccess mod_rewrite with 3 directories levels
How can I manage 3 subdirectories levels on Htaccess?如何在 Htaccess 上管理 3 个子目录级别?
Explanation:解释:
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 file is located in initial root folder (where index.php) .htaccess 文件位于初始根文件夹(其中 index.php)
Is the next the best way?下一个是最好的方法吗?
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]
You are fairly close.你相当接近。 Just note that
RewriteCond
is applicable to next immediate RewriteRule
only so you check of !-f
will only be used for first rule.请注意,
RewriteCond
仅适用于下一个直接RewriteRule
因此您检查!-f
将仅用于第一条规则。 You can have a separate rule to discard all requests that are for existing files and directories.您可以使用单独的规则来丢弃针对现有文件和目录的所有请求。
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.