htaccess 某些目录、子目录的用法

Htaccess some directory, subdirectory usage

我想重写 "cat/" => x.php "cat/sample-title" => y.php?t=$1 使用 htaccess。

我写了重写规则;

RewriteRule ^cat/ x.php [L]
RewriteRule ^cat/(.*)$ y.php?s=   [L]

但总是 cat/ 重写为 y.php?s=$1

您遇到了两个问题。第一个是第一条和第二条规则的模式都匹配^cat/,所以第二条在重写后取代了第一条。您将需要使用 $ 来锚定它,并且还可能希望允许使用 /?.

的可选尾随 /

然后,为了与第一个区分开来,第二个必须匹配 一个或多个 个字符 cat/,所以不是 (.*),你应该使用 (.+).

我也会更改他们的顺序(尽管在这种情况下并非绝对必要)

# Ensure one or more characters with .+
RewriteRule ^cat/(.+)$ y.php?s= [L]
# Anchored with optional trailing /
RewriteRule ^cat/?$ x.php [L]