.htaccess 重定向在漂亮 url 时会导致循环 可点击示例

问题描述

原因

我的 .htaccess 为每个子域提供来自不同目录的内容。浏览到该目录时,路径应该被删除并且只输出干净的 url。当子域从目录提供内容时,这会导致循环,这被检测为丑陋的 url 并被重写回 url,从文件夹中获取提供的 url,依此类推......

环境

我为我的 Web 服务器设置了 .htaccess 文件。网络服务器托管多个域及其各自的子域,文件夹中的每个子域如下:

├─ domains/
├─ subdom/
│  ├─ another.domain/
│  │  ├─ server/
│  │  │  ├─ index.htm
│  │  ├─ www/
│  │  │  ├─ index.htm
│  ├─ first.domain/
│  │  ├─ www/
│  │  │  ├─ index.htm
│  │  ├─ www2/
│  │  │  ├─ index.htm
.htaccess
index.htm

我正在尝试为来自 /subdom/first.domain/www(以及任何其他域或子域)的 www.first.domain 提供内容。这可以通过我的 .htaccess 中的第二块 RewriteConds 很好地实现。

内容也可以通过www.first.domain/subdom/first.domain/www(或first.domain/subdom/first.domain/www)访问,我不喜欢那样- 所以我还实现了一个代码来从 URI 中删除 subdom/any.domain/ 部分,并替换 url。这是由我的 .htaccess 文件中的第一块 RewriteConds 提供的。

问题

这导致我的 .htaccess 在循环中重写 url,从 www.first.domain 服务 /subdom/first.domain/www,.htaccess 检测到它是丑陋的 url 并将其重写回 www.first.domain,如此循环下去。

我正在寻找一种解决方案,只在需要时应用重定向,或者当 url 干净时 url 清理器不生效(例如,当内容别名时不触发)。

可点击示例

https://www.jozefmutis.com(提供来自 https://jozefmutis.com/subdom/jozefmutis.com/www内容https://jozefmutis.com/subdom/jozefmutis.com/www(应该被重写为 https://www.jozefmutis.comhttps://www.jozefmutis.com/subdom/jozefmutis.com/www(也应该重写为 https://www.jozefmutis.com

这是我的 .htaccess,我尝试使用 https://technicalseo.com/tools/htaccess/ 进行调试。

Options -Indexes

# remove subdom/domain.tld/ from uri
RewriteCond %{REQUEST_URI} subdom/([^\/]*)/
RewriteCond %{REQUEST_URI} /subdom/([^\/]*)/
RewriteCond %{REQUEST_URI} (.*)subdom/([^\/]*)/([^\/]*)
    RewriteRule (.*) https://%3.%2 [QSA]

# subdomains (with or w/o www at beginning)
RewriteCond %{REQUEST_URI} !^subdom/([^\/]*)/
RewriteCond %{REQUEST_URI} !^/subdom/([^\/]*)/
RewriteCond %{HTTP_HOST} ^(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2.%3/%1 -d
    RewriteRule (.*) subdom/%2.%3/%1/$1 [QSA,DPI]

# http > https
RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{SERVER_NAME}/$1 [R=301,L]
Header set Content-Security-Policy "upgrade-insecure-requests;"

# Favicon
    RewriteRule ^favicon.ico favicon.ico [L]

# Error 404
    ErrorDocument 404 /?Error404
# Error 500
    ErrorDocument 500 /?Error500

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)