我有这样的链接:
example.com/site/anyFolderName
我需要在这里重定向(静默):
example.com/site/myFolder/anyFolderName
保留所有子链接
example.com/site/anyFolderName/ (requests)--> example.com/site/myFolder/anyFolderName/ example.com/site/anyFolderName/picture.png (requests)--> example.com/site/myFolder/anyFolderName/picture.png example.com/site/anyFolderName/index.html (requests)--> example.com/site/myFolder/anyFolderName/index.html example.com/site/anyFolderName/page.php (requests)--> example.com/site/myFolder/anyFolderName/page.php
我的RewriteRule陷入了无限循环。任何可以指向正确方向的片段/教程/链接/想法都非常受欢迎!非常感谢!
发现这是真的:
答案 0 :(得分:1)
无限循环是因为毫无疑问,你无休止地匹配myFolder。需要从模式中排除它。由于匹配模式是Perl兼容的Reguar表达式,您可以使用负的零宽度断言(?!regex)
将此添加到服务器conf或.htaccess
RewriteEngine On
RewriteRule ^site/((?!myFolder/).*)$ site/myFolder/$1 [L,NC]
答案 1 :(得分:1)
这样做:
RewriteEngine on
RewriteBase /
#to prevent looping
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
#To handle trailing slash problem
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(?:\.\w+|/)$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(site)(/.*) $1/myfolder$2 [L,NC]
虽然在你的情况下
#to prevent looping
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
不仅仅是上述要求。
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
将负责循环终止。