apache - .htaccess 重写规则 : what is the differences between %1 and $1 variables (percentage vs dollar sign)?

标签 apache .htaccess mod-rewrite

到目前为止,我的所有重写规则都使用 $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/

相关文章:

apache - 具有 Apache 多个 VirtualHosts 的 Golang webapp

php - 灯 : Why php in apache will not race for the mysql db connection?

php - 404 page not found 您请求的页面在 codeigniter 中找不到

apache - 当 SSL 证书为非 www 时,在 https www 上将 www 重写为非 www 失败

.htaccess - Joomla 搜索返回无效的网络链接 URL(SEO 友好 URL 不起作用)

apache - 使用 ReWriteEngine 将用户个人资料页面重定向到物理页面

apache - 更改我的主机文件以路由到文件夹而不是IP

PHP readfile() 永远不会结束并使 Apache 服务器挂起

php - 在 URL 中省略 index.php 时,在 Slim Framework 中总是出现 404 错误

php - htaccess mod_rewrite 没有符号链接(symbolic link)?