到目前为止,我的所有重写规则都使用 $1、$2... 变量来形成一个干净的 URL。
现在我需要根据域名的一部分( http://subdir.example.com
=> http://example.com/subdir
)进行重定向,并且我发现许多示例使用 %1 变量而不是 $1 格式。我应该注意subdir
是任意字符串。
这对我来说还不清楚。我试图找出 $1 和 %1 变量的含义一两个小时,但仍然找不到任何有用的东西。
最佳答案
您可以在 .htaccess
中使用它:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdir\.example\.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteRule (.*) /subdir/$1
请确保在测试之前清除缓存。
OP 的建议编辑:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteCond %{REQUEST_URI} !^/????/
RewriteRule (.*) /????/$1
关于apache - .htaccess 重写规则 : what is the differences between %1 and $1 variables (percentage vs dollar sign)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41727528/