【发布时间】:2020-06-11 20:06:18
【问题描述】:
我试图用域 B 屏蔽域 A 的特定路由(以及其后的所有子路由)。所以基本上如果有人访问“DomainB.com”,他应该被带到“DomainA.com/page/1”。当有人访问“DomainB.com/about”时,他应该被带到“DomainA.com/page/1/about”。但是,在浏览器中应该始终存在“DomainB.com/about”。
我通过在域 B 的根文件夹内的 .htaccess 中使用以下行来实现这一点:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^DomainB\.com$ [NC]
RewriteRule ^(.*)$ https://DomainA.com/page/1/$1 [P]
现在所有子路由(例如 DomainB.com/about)都可以正常工作。但是索引路由(DomainB.com)给了我一个错误 404,我不知道为什么。
我在这个项目中使用 Laravel 7。 DomainA.com 的 htaccess 如下所示:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase /
# Allow access to font files for known hosts
<FilesMatch "\.(ttf|otf|eot|woff|woff2)$">
<IfModule mod_headers.c>
SetEnvIf Origin "http(s)?://(www\.)?(DomainB.com)$" AccessControlAllowOrigin=$0
Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
Header merge Vary Origin
</IfModule>
</FilesMatch>
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Gzip text compression
<IfModule mod_deflate.c>
# Compress HTML, CSS, JavaScript, Text, XML and fonts
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>
# Redirect to non-index.php url.
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^index.php(.*)$ /$1 [R=301,L]
</IfModule>
我的 .htaccess 有什么问题吗?或者有没有办法保留我编写的规则并编写第二条规则,仅将索引路由重定向到“DomainA.com/page/1”?
干杯, 保罗
【问题讨论】:
-
直接访问
https://DomainA.com/page/1/是否有效 - 还是仅在没有斜杠的情况下有效?如果是后者,那么您可能必须单独处理这种情况。 -
是的,访问
https://DomainA.com/page/1/和https://DomainA.com/page/1工作正常。 -
如果您将
P标志替换为R=302- 在尝试访问B 的根目录时,您是否会重定向 到正确的位置?跨度> -
是的!一旦我更改重定向方法,我就会进入正确的页面。不幸的是,这对我来说不是一个有效的解决方案,因为我必须在 URL 中保留 DomainB.com。
-
是的,我知道 - 只是想缩小这里出错的地方。接下来,我将继续检查域 A 的日志文件 - 检查来自代理的传入请求是什么样的,以及它与直接针对该 URL 发出的“正常”请求有何不同。