【问题标题】:laravel htaccess force wwwlaravel htaccess 强制 www
【发布时间】:2020-02-02 19:36:04
【问题描述】:

我有这个来自 laravel 6 的 .htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # force www ???

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

我想要的是从非 www 强制到 www

我尝试了几种组合但都不起作用(我得到“重定向你太多次”)

我尝试过的

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|offs()
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

如何强制 www ???

【问题讨论】:

  • 在浏览器中查看网络流量...你看到了什么?重定向的本质是什么?对于上述导致重定向,这意味着有 其他东西 将您重定向回域顶点(非 www)?您的代码中有任何重定向吗?
  • @DocRoot 我什么都没看到……只有这个 The page isn’t redirecting properly 在 Firefox 中。我的代码中没有重定向。我还将 APP_URL 从 .env 更改为 APP_URL=http://www.example.com 而不是 http://example.com

标签: laravel apache .htaccess


【解决方案1】:

也试试

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.yourwebsite.com/$1 [L,R=301]

RewriteCond %{HTTP} off
RewriteRule ^(.*)$ http://www.yourwebsite.com/$1 [L,R=301] 

【讨论】:

  • 不工作:我在 Firefox The page isn’t redirecting properly 上收到此消息
  • 这也是 OP 未提及或在其代码中使用的 HTTP 到 HTTPS 重定向。
猜你喜欢
  • 2016-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
  • 2023-03-27
  • 1970-01-01
相关资源
最近更新 更多