apache - Laravel 将 www 重定向到非 www 且 htaccess 无法正常工作
问题描述
.htaccess
我在将 www URL 重定向到非 www URL的文件中有以下行:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
尝试加载 index route: https://www.example.com
to时它可以正常工作https://example.com
,但是如果有人尝试加载任何其他路由,它不会加载任何内容,例如,如果我尝试加载https://www.example.com/about-us
它将 URL 转换为https://example.com/index.php
并且它不会加载任何内容。
我怎样才能解决这个问题?
编辑
我的完整.htaccess
如下:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(\.css|\.pdf|\.mp4|\.woff|\.woff2|\.ttf|\.js|\.png|\.jpg|\.jpeg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images|favicon|fonts|font|videos|storage|pdf)/(.*)$ public/$1/$2 [L,NC]
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>
<FilesMatch ".(eot|ttf|otf|woff|woff2)">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
<Files .env>
order allow,deny
Deny from all
</Files>
解决方案
你可以试试这个
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [L,NE,R=301]
推荐阅读
- python - 提高 pandas 中的行拆分效率
- python - 我试图弄清楚如何从我的 csv 文件中删除逗号和美元符号
- android - LiveData 未在向 Room DB 添加新条目时调用观察者
- php - 警告:preg_replace_callback():需要参数 2,'word(\1)',作为第 54 行 /.php 中的有效回调
- azure-data-factory-2 - 如何从 Azure Blob 存储中读取文件夹结构为“StartDateOfMonth-EndDatefMonth”的文件?
- reactjs - 从 git repo 依赖项导入反应组件时出错:元素类型无效
- android - 如何在时间选择器中将上午和下午更改为大写?
- authentication - salt-stack 中的软件包安装,包括 --allow-unauthenticated
- perl - 如何在 perl 中添加库的相对路径?
- keras - 为什么 Keras 的注意力模型只有一个输入?