.htaccess重定向而不保留旧站点,http到https

时间:2017-09-19 09:43:56

标签: .htaccess redirect

我想请求您对我的新网站页面重定向的帮助和意见。 我有一个古老的网站,有旧的设计和太复杂的等等,现在我做了一个新的,但旧的有一个非常好的谷歌搜索的位置,所以我想保持这个位置。 我使用.htaccess将我的旧页面重定向到这样的新页面:

redirect 301 /oldwebsite/oldfile.html https://www.newsite.com/index.php

这是有效的,但我有一些疑问和问题:

1,旧的网站页面是.html页面,新的全部是.php

2,旧网站为http://www.mywebsite.com/index.html,但现在我制作了,我想将网页重定向到https://www.mywebsite.com/index.php

3,如果可能的话我不想把旧页面当作“oldwebsite”这样的文件夹,并进行如下的重定向:

redirect 301 /oldwebsite/oldfile.html https://www.newsite.com/index.php

如果可能,我想删除旧页面(完全),因为它在网络托管上有很多空间。

4,在旧网站上我有太多.html页面,例如material1.html,material2.html .... material32.html,我想将文件重定向到只有一个新页面。

也许是这样的,但我认为这是不对的:

redirect 301 /oldwebsite/materials/material1.html https://www.newsite.com/materials.php
redirect 301 /oldwebsite/materials/material2.html https://www.newsite.com/materials.php
redirect 301 /oldwebsite/materials/material3.html https://www.newsite.com/materials.php

。 。

redirect 301 /oldwebsite/materials/material3223.html https://www.newsite.com/materials.php

我对.htaccess没有太多经验但是,作为一个外行人,我考虑过它,但它不起作用:

redirect 301 http://www.mywebsite/contact.html https://www.mywebsite.com/contact.php

然后删除http://www.mywebsite/contact.html页面只是在.htaccess文件中写一行,因为如果有人记得我的旧页面并在浏览器上输入它可能会重定向新网站,即使旧的文件已从存储中删除。

这就是我的想法,但不幸的是,我还没有完全实现它。

3 个答案:

答案 0 :(得分:0)

将以下代码放在旧网站主目录.htaccess

RewriteEngine On
RewriteRule ^(.*)$  https://www.newsite.com/$1 [L,R=302]

因此,现在您将任何请求重定向到旧网站https新网站。

转到新站点并将以下代码放在主目录.htaccess文件中。

RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.html[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
ErrorDocument 404 https://www.newsite.com/404.php

旧网站将被重定向到新网站,在SEO和排名方面,在您的情况下,排名不受301永久重定向的影响,除非旧网站中的某些网页不在新网站上并且没有规则来处理新网站中的错误文档以及我添加此行ErrorDocument 404 https://www.newsite.com/404.php.html扩展名的原因我将此行添加到任何.html并删除它,因此如果用户请求http://www.oldsite.com/somepage.html它将转到https://www.newsite.com/somepage,如果有somepage.php则确定,否则将被视为错误并转到错误页面“404.php”。

测试代码,如果每件事情都正常,请在旧网站.htaccess中更改此行:

RewriteRule ^(.*)$  https://www.newsite.com/$1 [L,R=302]

用这个:

RewriteRule ^(.*)$  https://www.newsite.com/$1 [L,R=301]

现在整个旧网站将永久定向到新网站。

答案 1 :(得分:0)

谢谢你的回答! 现在我有我的网站,如:http://www.mywebsite.com 它有旧的设计,.html页面等。

新网站仍将保留在此域只删除旧文件,将其重定向到新创建的.php文件并从http切换到https,但网站的名称和可用性将保持https://www.mywebsite.com。 所以,我只有一个htaccess文件,我只需要将旧的html文件重定向到php文件,如果可能的话,删除这些旧的html文件,以及如果有人正在寻找我的网站,那么重定向到一个新的php文件。

如果我在你的回答中理解,如果我想将旧域的.html页面重定向到一个全新域名的.php页面,这将被称为http而不是https。对我来说,这个域名将被保留,但我想完全删除旧的.html站点并将其更改为新的.php站点。

答案 2 :(得分:0)

与此同时,我试图解决它并且以下解决方案有效,但我不知道它是否正确。

这是我的.htaccess文件的内容:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
# Don't put www. here. If it is already there it will be included, if not
# the subsequent rule will catch it.
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
# [NC] is a case-insensitive match
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

ErrorDocument 404 /error.php

redirect 301 /oldsite_1.html https://www.mywebsite.com/contact.php
redirect 301 /oldsite_2.html https://www.mywebsite.com/index.php
.
.
.

所以现在我的存储包含旧的.html文件和新的.php文件。 如果我正在寻找旧网站转到http://www.mywebsite.com/oldsite_1.html,它会重定向到https://www.mywebsite.com/contact.php。如果我从存储中删除oldsite_1.html文件(这将是不在存储中保留旧文件的最终目标),这也有效。

这是一个很好的解决方案吗? 在谷歌搜索期间,我的网站不会丢失这个旧位置(即使我删除旧文件)?

相关问题