保留重定向时,从URL中删除目录
我有一个重定向。如果请求插入根,并且内容可在 /网站3中可用,则可以从网站3中提取内容,但仅显示root请求。这真的很好。例如,一个请求是示例com.com/foo/test/,并且在/weblity3/foo/test inter inter neblition 3中有一个index.htm文件,而无需在URL中包括exites3。如果存在类似示例com.com/test之类的不同路径,它也会访问测试目录,也可以很好地发挥作用。这都是完美的。
我正在尝试考虑是否提出请求。我尝试了几件事,它们都导致循环。
这是当前代码:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Remove trailing slash on any URL that is requested directly (excludes rewritten URLs)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*)/$ /$1 [R=301,L]
# Rewrite root
RewriteRule ^$ website3/ [L]
# If request maps to a directory in "/website3" then rewrite and append trailing slash
RewriteCond %{DOCUMENT_ROOT}/website3/$1 -d
RewriteRule ^([^.]+)$ website3/$1/ [L]
RewriteCond %{DOCUMENT_ROOT}/website3/$1 -f
RewriteRule (.*) website3/$1 [L]
I have a redirect in place. If a request comes in to the root and the content is available in /website3 it pulls the content in from website3 but only displays the root request. This works really well. For example is a request comes in for example.com/foo/test/ and there is an index.htm file in /website3/foo/test the content displays without including website3 in the url. It also plays nicely if a different path exists like example.com/test it will go to the test directory. This is all perfect.
I am trying to factor out if the request comes in for example.com/website3/foo/test I would like this to redirect to example.com/foo/test but still pull in the content from website3. I have tried a few things and they all result in a loop.
Here is the current code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# Remove trailing slash on any URL that is requested directly (excludes rewritten URLs)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*)/$ /$1 [R=301,L]
# Rewrite root
RewriteRule ^$ website3/ [L]
# If request maps to a directory in "/website3" then rewrite and append trailing slash
RewriteCond %{DOCUMENT_ROOT}/website3/$1 -d
RewriteRule ^([^.]+)$ website3/$1/ [L]
RewriteCond %{DOCUMENT_ROOT}/website3/$1 -f
RewriteRule (.*) website3/$1 [L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
RewriteEngine
指令之后,立即添加以下内容,从直接请求的任何URL开始时删除/werfe 3/code>。
请注意,这会检查
redirect_status
env var,以防止重定向循环 - 与您的规则相同的原理以删除后斜线。redirect_status
env var未在初始请求上设置(即空),并在第一个成功重写之后设置为“ 200”(如200个OK HTTP状态)。首先用302(临时)重定向测试,以避免潜在的缓存问题。
Add the following immediately after the
RewriteEngine
directive to remove/website3/
from the start of any URL that is requested directly.Note that this checks the
REDIRECT_STATUS
env var in order to prevent a redirect loop - the same principle as your rule to remove the trailing slash.The
REDIRECT_STATUS
env var is not set (ie. is empty) on the initial request and set to "200" (as in 200 OK HTTP status) after the first successful rewrite.Test first with a 302 (temporary) redirect to avoid potential caching issues.