Apache htaccess将RewriteBase设置为请求URI的开头
如何将.htaccess文件中的RewriteBase设置为请求URI的第一部分(domain.com/path或domain.com/path/to/file会将RewriteBase设置为/path)?您不能动态设置Apache htaccess将RewriteBase设置为请求URI的开头,apache,.htaccess,mod-rewrite,Apache,.htaccess,Mod Rewrite,如何将.htaccess文件中的RewriteBase设置为请求URI的第一部分(domain.com/path或domain.com/path/to/file会将RewriteBase设置为/path)?您不能动态设置RewriteBase,但您可以手动从%{REQUEST\u URI}中提取第一条路径并手动将其添加到绝对URL路径目标。例如: RewriteBase / RewriteCond %{REQUEST_URI} ^/([^/]+) RewriteRule ^foo$ /%1/ba
RewriteBase
,但您可以手动从%{REQUEST\u URI}中提取第一条路径
并手动将其添加到绝对URL路径目标。例如:
RewriteBase /
RewriteCond %{REQUEST_URI} ^/([^/]+)
RewriteRule ^foo$ /%1/bar.html [L]
在这里,如果该规则位于URL路径/a/b/c
的htaccess文件中,并且您请求/a/b/c/foo
,则该规则将重写为/a/bar.html
或者,如果您有很多规则,并且绝对需要使用%
反向引用,则可以将其设置为htaccess文件顶部的环境变量:
RewriteBase /
RewriteCond %{ENV:first_uri_path} ^$
RewriteCond %{REQUEST_URI} ^/([^/]+)
RewriteRule ^ - [E=first_uri_path:%1]
# then your rules:
RewriteCond %{HTTP_HOST} www\.(.*)
RewriteRule ^foo$ /%{ENV:first_uri_path}/bar.php?domain=%1 [L]
不确定此解决方案对您的可行性,具体取决于您实际计划如何使用“动态重写库”。是的,我希望将其设置为第一个文件夹。我在想如何更好地表达它时遇到了困难。如果你有任何问题可以帮助更好地定义它,我很乐意回答不是我真正希望听到的,但从我的研究来看,似乎没有更好的方法。谢谢你的回答!2020年-htaccess中仍然没有进一步可用的变量:-(