Apache - 如何将 http 请求仅转换为 https?

Apache - how to make http requests into https only?

如何判断是否 http://ttt.com or http://www.ttt.com is used by the user, redirect it to https://www.ttt.com ?

httpd.conf:

<VirtualHost *:80>
 ServerName www.ttt.com
 ServerAlias ttt.com
 DocumentRoot /home/www/html/ttt/public
 <Directory /home/www/html/ttt/public>
    #Options ExecCGI
    #AddDefaultCharset utf-8
    DirectoryIndex index.php
    AllowOverride All
    Order allow,deny
    Allow from all
 </Directory>
</VirtualHost>

.htaccess :

RewriteEngine On
############################################
## always send 404 on missing files in these folders
#RewriteCond %{REQUEST_URI} !^/(files)/
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

给你:https://wiki.apache.org/httpd/RedirectSSL

Redirect Request to SSL

Let's say you want http://www.example.com/secure/ to always be sent over SSL (I presume here that both the normal and the SSL vhost have the same content). You could do this by linking to the correct page from within your HTML pages... but there will always be some user who will sneak by it that way.

Using virtual hosts (using redirect)

When using SSL, you will frequently have at least two virtual hosts: one on port 80 to serve ordinary requests, and one on port 443 to serve SSL. If you wish to redirect users from the non-secure site to the SSL site, you can use an ordinary Redirect directive inside the non-secure VirtualHost:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName mysite.example.com
   DocumentRoot /usr/local/apache2/htdocs
   Redirect permanent /secure https://mysite.example.com/secure
</VirtualHost>

<VirtualHost _default_:443>
   ServerName mysite.example.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On
# etc...
</VirtualHost>

When redirecting everything you don't even need a DocumentRoot:

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName www.example.com
   Redirect permanent / https://secure.example.com/
</VirtualHost>

<VirtualHost _default_:443>
   ServerName secure.example.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On
# etc...
</VirtualHost>

Note: redirect can also be used inside .htaccess files or to address particular URLs, as in:

Example:

Redirect permanent /login https://mysite.example.com/login

使用mod_rewrite

建议使用该解决方案,因为它更简单 更安全,你也可以使用 mod_rewrite 来获得相同的效果 此处描述:RewriteHTTPToHTTPS

来自https://httpd.apache.org/docs/trunk/mod/mod_alias.html#redirect

# Redirect to a URL on a different host Redirect "/service" "http://foo2.example.com/service"

# Redirect to a URL on the same host Redirect "/one" "/two"

If the client requests http://example.com/service/foo.txt, it will be told to access http://foo2.example.com/service/foo.txt instead. This includes requests with GET parameters, such as http://example.com/service/foo.pl?q=23&a=42, it will be redirected to http://foo2.example.com/service/foo.pl?q=23&a=42. Note that POSTs will be discarded. Only complete path segments are matched, so the above example would not match a request for http://example.com/servicefoo.txt. For more complex matching using the expression syntax, omit the URL-path argument as described below. Alternatively, for matching using regular expressions, see the RedirectMatch directive.

如果您希望 www.example.com/*example.com/* 都被重定向,您可以创建两个具有不同 ServerName 的 VirtualHost,或者您可以使用 Rewrite 插件。

在主目录 .htaccess 文件中尝试以下代码:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]

您可以在 httpd.conf 中执行以下操作:

<VirtualHost *:80>
    ServerName www.ttt.com
    Redirect "/" "https://www.ttt.com/"
</VirtualHost>

<VirtualHost *:443>
    ServerName www.ttt.com
    ...
    ...
</VirtualHost>

或来自 .htaccess 文件:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]