【发布时间】:2021-03-26 03:08:34
【问题描述】:
我有 .htaccess 设置来获取对 www 的任何非 www 请求。
它适用于主页,但是当我检查其他 URL 时,它会转到主页。
这是我的设置(已更新):顶部来自 laravel
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# 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>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
</IfModule>
例如
https://example.com 转到https://www.example.com。
但是https://example.com/contact 不会转到https://www.example.com/contact。相反,它转到https://www.example.com。
可能是什么问题和解决方案?
【问题讨论】:
-
您如何路由
/contact的请求? (或者/contact的请求映射到什么?)您的配置中似乎缺少一些指令? -
我已将我拥有的部分放在顶部进行了编辑。现在可能更清楚了。
-
另一方面,我在 cpanel 上自动创建了这个重定向。 i.imgur.com/60YyZlB.png
-
是的,您的指令顺序错误,正如我的回答中已经解释的那样。该 cPanel 重定向已包含在您发布的指令中。 (这就是 cPanel 为您所做的一切 - 它编辑
.htaccess文件,但它添加的重定向始终位于文件末尾 - 这通常是错误的位置。)
标签: laravel .htaccess mod-rewrite