简体   繁体   English

仅在具有.htaccess的特定页面上使用HTTPS

[英]HTTPS only on specific page with .htaccess

I have a URL such as http://www.domain.com/index.php?p=register . 我有一个URL,例如http://www.domain.com/index.php?p=register I want to redirect that to use HTTPS (SSL) with .htaccess, but only on this, and a couple of other pages (the login page, etc), but not the entire site. 我想重定向使用HTTPS(SSL)与.htaccess,但仅​​限于此,以及其他几个页面(登录页面等),但不是整个站点。 The URLs don't point to directories, but are used to dynamically include different files. URL不指向目录,但用于动态包含不同的文件。

Can someone give me a pointer or an example of how to get a single page redirect to HTTPS please? 有人可以给我一个指针或一个如何让单个页面重定向到HTTPS的例子吗?

Thanks. 谢谢。

Not htaccess, but another way could be to use PHP to redirect: 不是htaccess,但另一种方法可能是使用PHP重定向:

<?php

$redirectlist = array('register','login','myaccount');

if (in_array($_GET['p'], $redirectlist) && strtolower($_SERVER['HTTPS']) != 'on') {
    exit(header("location: https://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}"));
}

?>

The only reason I mention this is that, in some cases, this may be easier to maintain than a separate htaccess. 我提到这一点的唯一原因是,在某些情况下,这可能比单独的htaccess更容易维护。 You would need to put this in place in your PHP content before any text was outputted (see header() ). 在输出任何文本之前,您需要在PHP内容中放置它(请参阅header() )。

something like this should work: 这样的事情应该有效:

RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{QUERY_STRING} (^|&)p=(register|login|or|other|protected|page)($|&)
RewriteRule (.*) https://www.domain.com/index.php [R=301,QSA,L]

Some explanation: 一些解释:

  1. Check if server port is different from 443 (standard for secure connections), to ensure we are going to redirect only non-secured connections 检查服务器端口是否与443(安全连接标准)不同,以确保我们仅重定向非安全连接
  2. The query string (everything after ?) have to match pattern: include p variable with one value from pipe separated list 查询字符串(后面的所有内容?)必须匹配模式:包含p变量和管道分隔列表中的一个值
  3. Redirect everything to secure domain, sending 301 response status, appending all query string and marking is as last rule, so any rules below this will not be parsed (since this is redirect, we don't want to take any other actions) 将所有内容重定向到安全域,发送301响应状态,附加所有查询字符串和标记是最后一条规则,因此下面的任何规则都不会被解析(因为这是重定向,我们不想采取任何其他操作)

If you have option to follow the php method, I would recommend to follow that or with any other dynamic languages. 如果您可以选择遵循php方法,我建议您遵循该方法或使用任何其他动态语言。 You must avoid using htaccess since links to images, js and other contact on that page will be forced to be nonSSL and modern browsers would show a non-compliance sign which might look a whitewash over your SSL cost. 您必须避免使用htaccess,因为该页面上的图像,js和其他联系人的链接将被强制为非SSL,而现代浏览器将显示不合规标志,这可能看起来对您的SSL成本进行粉饰。

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

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