首页 > 解决方案 > 删除 /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 个可能的结果。

标签: .htaccessapache2

解决方案


我认为这可以完成这项工作

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那将保留

在此处输入图像描述


在此处输入图像描述


推荐阅读