Apache - how to make http requests into https only

2019-07-24 21:11发布

This question already has an answer here:

How to tell if 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]

3条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-24 21:44

Here you go: 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

Using mod_rewrite

While the solution is recommended because it is simpler and safer, you can also use mod_rewrite to get the same effect as described here: RewriteHTTPToHTTPS

From 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.

If you want both the www.example.com/* and the example.com/* to be redirected you could make two VirtualHost with different ServerName or you can use the Rewrite plugin.

查看更多
放荡不羁爱自由
3楼-- · 2019-07-24 21:48

Try the following Code at the main directory .htaccess file :

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
查看更多
爷的心禁止访问
4楼-- · 2019-07-24 22:01

You can do it from the httpd.conf with the following:

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

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

Or from the .htaccess file:

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
查看更多
登录 后发表回答