首页 > 解决方案 > 以不同的方式处理 htaccess apache 中的 2 个类似 url

问题描述

大家好,我需要有关 apache htaccess 的帮助。我有这 2 个网址:

我如何以不同的方式捕捉这两种情况?

实际上我有这个htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} !^/test/admin[NC]
RewriteCond %{REQUEST_URI} !^/test/(it|en) [NC]
RewriteRule ^(.*)$ /test/en/$1 [R=301,L,QSA]

RewriteRule ^([\w-]+)/([\w-]+)/(\d+)/([\w-]*)$ /test/index.php?detMod=$3 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]*)$ /test/index.php?page=$2&category=$3 [L,QSA,NC]

但是对于数字(1564)而不是(guns2)的情况,我需要另一条规则

标签: .htaccessurl-rewriting

解决方案


用于\d+仅匹配数字并[\w-]+用于匹配字母数字,_以及-

^([\w-]+)/([\w-]+)/(\d+)/([\w-]*)$

^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]*)$

建议的.htaccess:

RewriteCond %{REQUEST_URI} !^/test/admin[NC]
RewriteCond %{REQUEST_URI} !^/test/(it|en) [NC]
RewriteRule ^(.*)$ /test/en/$1 [R=301,L]

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

RewriteRule ^([\w-]+)/([\w-]+)/(\d+)/([\w-]*)$ test/index.php?detMod=$3 [L,QSA,NC]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]*)$ test/index.php?page=$2&category=$3 [L,QSA,NC]

推荐阅读