保留重定向时,从URL中删除目录

发布于 2025-01-26 02:57:46 字数 854 浏览 1 评论 0原文

我有一个重定向。如果请求插入根,并且内容可在 /网站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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸵鸟症 2025-02-02 02:57:46

RewriteEngine指令之后,立即添加以下内容,从直接请求的任何URL开始时删除/werfe 3/code>。

# Remove "/website3/" from any URL requested directly
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^website3/(.*) /$1 [R=301,L]

请注意,这会检查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.

# Remove "/website3/" from any URL requested directly
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^website3/(.*) /$1 [R=301,L]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文