【问题标题】:Redirect all (sub-)routes in .htaccess and keeping url results in error 404 on index page重定向 .htaccess 中的所有(子)路由并保留 url 在索引页面上导致错误 404
【发布时间】: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 发出的“正常”请求有何不同。

标签: php laravel .htaccess


【解决方案1】:

我找到了一种解决方法,但这对我来说不是理想的解决方案。

首先,我声明了一个名为“/index”的新路由,指向与“/”路由指向的函数相同的函数。像这样:

Route::get('/index', 'PageController@index'); // the /page/{id} part is done in the group above with model binding

然后我将以下行添加到我的 .htaccess 中:

    RewriteCond %{HTTP_HOST} DomainB\.com$ [NC]
    RewriteCond %{REQUEST_URI} ^/$
    Rewriterule ^(.*)$ https://DomainB.com/index [L,R=301]

这会将用户从 DomainB.com 重定向到 DomainB.com/index,这是可行的。但是,我不知道这是否是一个理想的解决方案,因为更改索引链接可能对 SEO 不利……这对 DomainA.com 上的 SEO 肯定不利,因为现在有多种方法可以访问索引页面。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    • 2020-05-16
    • 2014-12-14
    相关资源
    最近更新 更多