.htaccess - 如何在有和没有 www 的情况下从 http 重定向到 https 并具有规范的 url
问题描述
以前可能有人问过这个问题,但我只是想确保在我的情况下它是正确的。
基本上我在 PageChecking 界面上几乎没有错误,我可能有 2 个页面可用,这些页面可能会重复内容。目前我有来自基本 HTTP 的 HTTPS 重定向。
尽管我需要将 www 重定向到非 www https 并保留非 www 地址并添加 https 的重定向。
这就是我的 htaccess 的样子:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.website.com$
RewriteCond %{HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_REFERER} ^http://www.ownedcore/upl/usr/ [NC]
RewriteCond %{HTTP_REFERER} ^http://www.bananaforum.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http://www.dafont.com/[NC]
RewriteCond %{HTTP_REFERER} ^http://goingsony.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http://i.imgur.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http://www.wallpapervortex.com/ [NC]
RewriteRule . https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
这些与其他 url 的 rewritecond 可能无关紧要,所以请让我知道我应该删除和修复什么,谢谢!
解决方案
在 .htaccess 文件顶部检查此修改后的规则
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule (.*) https://website.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_REFERER} ^http://www.ownedcore/upl/usr/ [NC]
RewriteCond %{HTTP_REFERER} ^http://www.bananaforum.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http://www.dafont.com/[NC]
RewriteCond %{HTTP_REFERER} ^http://goingsony.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http://i.imgur.com/ [NC]
RewriteCond %{HTTP_REFERER} ^http://www.wallpapervortex.com/ [NC]
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
推荐阅读
- java - GraphQL 查询解析器作为单独的 Web 事务
- python - 首次尝试使用导入请求时 Python 连接被拒绝
- google-bigquery - 如何在不使用 case 语句的情况下在 BigQuery 中创建数据透视表
- plc - 为什么我不能将输出变量作为参数传递给 IEC61131-3 结构化文本 (TwinCAT3) 中的功能块构造函数?
- elasticsearch - 创建一个包含 N 个区间为 X 的桶的直方图,其中最后一个桶还包括大于其区间的计数
- python-3.x - 更改 Pandas 数据框中的列值以将数字显示为 Milions
- ajax - 断开互联网时,Ajax getElementById 停止工作
- ruby-on-rails - before_action :find_transaction 不起作用 rails api
- visual-studio-code - CPP 调试器遇到断点,但错误的源文件映射导致许多“无法解析不存在的文件”弹出窗口
- sqlite - QueryFailedError: SQLITE_MISUSE: 数据库句柄已关闭(sqllite memory Jest 测试)