Apache 重写URL,以便从URI中删除第一个文件夹
问题 我试图编写一个重写规则来重写这个Apache 重写URL,以便从URI中删除第一个文件夹,apache,.htaccess,mod-rewrite,url-rewriting,Apache,.htaccess,Mod Rewrite,Url Rewriting,问题 我试图编写一个重写规则来重写这个 http://my.domain.pl/subdir/a/b/c/d/foo.bar http://my.domain.pl/subdir/a/b/c/d/foo.bar 进入这个 http://my.domain.pl/subdir/b/c/d/foo.bar http://my.domain.pl/subdir/b/c/d/ghi/hij/foo.bar http://my.domain.pl/subdir/b/c/d/fooxbar 目标U
http://my.domain.pl/subdir/a/b/c/d/foo.bar
http://my.domain.pl/subdir/a/b/c/d/foo.bar
进入这个
http://my.domain.pl/subdir/b/c/d/foo.bar
http://my.domain.pl/subdir/b/c/d/ghi/hij/foo.bar
http://my.domain.pl/subdir/b/c/d/fooxbar
目标URL中没有a/
文件夹
subdir
是放置.htaccess的文件夹。该规则必须删除子目录
旁边的第一个文件夹,无论路径中该文件夹后面有多少个文件夹
比如这个
http://my.domain.pl/subdir/a/b/c/d/ghi/hij/foo.bar
应该改写成这个
http://my.domain.pl/subdir/b/c/d/ghi/hij/foo.bar
还有
我的方法
我准备了规则,但它没有像我预期的那样起作用:
RewriteRule ^[^/]+/(.+)\.([^/]+)$ $1.$2 [L]
这条规则被改写了
http://my.domain.pl/subdir/a/b/c/d/foo.bar
进入
奇怪的是,一个非常相似的规则,在$1.$2
中没有句号,但没有像我预期的那样重写一封信。我指的是这样一条规则:
RewriteRule ^[^/]+/(.+)\.([^/]+)$ $1x$2 [L]
它重写了这个
进入这个
http://my.domain.pl/subdir/b/c/d/foo.bar
http://my.domain.pl/subdir/b/c/d/ghi/hij/foo.bar
http://my.domain.pl/subdir/b/c/d/fooxbar
像这样转义规则中的句点$1\.$2
没有帮助
问题
如何创建规则以删除路径的第一个文件夹?您没有使用正确的正则表达式模式来匹配请求的uri。请尝试以下操作:
RewriteRule ^[^/]+/(.*?)/(.+\.[^/]+)$ /subdir/$1/$2 [L]
它不起作用。此路径收缩在循环中执行多次。我的规则也有同样的问题。因此,我们必须制定一个规则,只处理收缩的。我们可以添加一个RuleCond来检查
a
文件夹是否存在,并在它只存在时进行重写,但文件夹的名称应该是已知的。