简体   繁体   English

301从http重定向到https相同的页面名称

[英]301 Redirect from http to https same page name

checked the Forum but could not find an ideal answer. 检查了论坛,但找不到理想的答案。 I have recently installed a SSL Certificate on my site and in the process of creating 301 redirects via the .htaccess file for nearly 400 page urls (to keep Google happy). 我最近在自己的网站上安装了SSL证书,并正在通过.htaccess文件创建301重定向,用于将近400个页面网址(以使Google满意)。 I thought of using; 我想到了使用;

redirect 301 /contact.php https://www.mydomainname.co.uk/contact.php

but it breaks the site. 但它破坏了网站。 The only solution I have seen is; 我看到的唯一解决方案是;

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^contact\.php$ https://www.mydomainname.co.uk/contact.php [L,R=301]

The above seems a lot of code to use for each of the 400 pages! 上面的代码似乎可用于400页中的每页! is there a quicker way with less code I can use in the .htaccess file? 有没有可以在.htaccess文件中使用较少代码的更快方法?

Many thanks. 非常感谢。 Hope someone can advise. 希望有人可以指教。

There are two basic ways of redirecting pages with Apache: Redirect (of mod_alias ) and RewriteRule etc. (of mod_rewrite ). 使用Apache重定向页面的基本方法有两种: Redirectmod_alias )和RewriteRule等( mod_rewrite )。

Redirect is very simple: it will just redirect a single URL to another. Redirect 非常简单:它将仅将单个URL重定向到另一个URL。 It can be useful sometimes, but it's usefulness is limited to its simplicity: in the case of HTTP-to-HTTPS redirection, it can't differentiate between HTTP and HTTPS connections, so it will just try to redirect to HTTPS even if you're already on HTTPS (and thus you end up in an infinite redirect loop). 有时它可能有用,但它的实用性仅限于其简单性:在HTTP到HTTPS重定向的情况下,它无法区分HTTP和HTTPS连接,因此即使您尝试将其重定向到HTTPS,已经在HTTPS上(因此您最终陷入无限重定向循环)。

RewriteRule , on the other hand, is more advanced and flexible. 另一方面, RewriteRule更高级,更灵活。 You can use RewriteCond to conditionally redirect requests; 您可以使用RewriteCond有条件地重定向请求。 in your case, you'd want to redirect requests only if they're on a HTTP connection. 在您的情况下,仅当请求位于HTTP连接上时,您才希望将其重定向。

As you mentioned, you want to redirect to HTTPS for many (I presume all) requests; 如您所提到的,您希望针对许多(我认为是全部)请求重定向到HTTPS。 you can easily do this with only a single rule: 您只需一个规则即可轻松完成此操作:

# Enable rewrites
RewriteEngine on

# Only run next RewriteRule on HTTP connections (not HTTPS)
RewriteCond ${HTTPS} off

# Redirect any page to the same URL with https:// schema
RewriteRule (.*) https://${SERVER_NAME}/$1 [L,R=301]

(The ${SERVER_NAME} variable will automatically be equal to your domain name, so you can even use this on web servers with multiple domain names.) ${SERVER_NAME}变量将自动等于您的域名,因此您甚至可以在具有多个域名的Web服务器上使用它。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM