.htaccess - 删除 /index 并将所有主页重定向到一个主页
问题描述
基本上,根据谷歌,我有 4 个“主页”。它们是: https://website.com/index https://www.website.com/index https://www.website.com/ https://website.com
我想让他们都指向一个,https://www.website.com/
为此,我需要找到一种方法来删除 /index,并将所有其他页面定向到该页面,但是我在正确设置 .htaccess 时遇到了问题。任何意见,将不胜感激。我将如何实现这种效果?
这是我已经在 .htaccess 中尝试过的内容:
`AddHandler application/x-httpd-eig-php52 .php
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Redirect 301 https://website.com/index https://www.website.com/
# Use PHP5.4 as default
#AddHandler application/x-httpd-php54 .php
#Options -Indexes
<FilesMatch "\.(ico|pdf|jpg|jpeg|png|gif|html|htm|xml|txt|css|js|mp4)$">
Header set Cache-Control "max-age=31536050"
</FilesMatch>`
我基本上只是想设置它,以便您登陆的唯一主页是https://www.website.com,而不是 4 个可能的结果。
解决方案
我认为这可以完成这项工作
RewriteEngine On
# Remove 'index' from link
RewriteCond %{REQUEST_URI} /index
RewriteRule ^(.*)$ https://www.website.com/ [R=301,L]
# Add 'www' and preserve all other pages in link
RewriteCond %{HTTP_HOST} ^website.com(.*)$ [NC]
RewriteRule ^(.*)$ https://www.website.com/$1 [R=301,L]
我在这个网站上测试过
它将全部重定向到https://www.website.com/
, nonwww
和 with/index
并且如果你有其他的东西/index
, 例如/some_page
那将保留
推荐阅读
- swift - 如何更新此 Hashable.hashValue 以符合新要求。?
- python - 如何以 Pythonic 方式向上移动 n 个目录?
- c - 在 C 中以增量方式读取和写入长数据类型时出错
- cakephp - 在 Apache 上的 https 上的 Mod 重写不起作用
- http-get - KotlinJs - 没有动态类型功能的简单 HTTP GET
- c - 从用户态程序读取内核堆栈
- json - 如何在弹性搜索中存储星型数据库
- oracle-apex - 如何修复 Oracle Apex 18.2 中的 Textarea 字段帮助文本(?)位置?
- c# - 在 Visual Studio 2017 安装项目中将 .NET Core 添加到先决条件
- charts - 付费图表未显示在 PDF 中