启用 HTTPS 并将 www.localhost 设置为默认应用程序 URL


Enable HTTPS and Set www.localhost as default application URL

我需要通过以下URL访问Web应用程序:

https://www.localhost/index.php

下面是我的 Apache 配置:

ScriptAlias /www.localhost/ "/var/www/siva/scripts/"
Alias /pages/ "/var/www/siva/wpages/"
ServerName www.localhost
<Directory "/var/www/siva/scripts">
    AllowOverride None
    AddType application/x-httpd-php .php .phtml
    AddType application/x-httpd-php-source .phps
    Options None
</Directory>
<Directory "/var/www/siva/wpages">
    AllowOverride None
    Options None
</Directory>
/

var/www/siva/scripts/index.php

<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html> 

如果我在浏览器中以 URL 身份访问我的页面:

http://localhost/www.localhost/index.php

它工作正常,但如果我访问

http://www.localhost/index.php

我应该进行哪些更改才能实现相同的目标,以及如何使其仅在"https"协议中可用?

Prakash,你需要添加ServerAlias才能让它也像localhost一样工作,并添加HTTPS重定向,RewriteRule,据我所知,这就是你想要的,例如:

ScriptAlias /www.localhost/ "/var/www/siva/scripts/"
Alias /pages/ "/var/www/siva/wpages/"
ServerName www.localhost
ServerAlias localhost
RewriteEngine On
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]

httpd ServerAlias 指令:

ServerAlias 指令设置主机的备用名称,以便与基于名称的虚拟主机一起使用。服务器别名可能包含通配符(如果适用)。

编辑 1:

首先,在编辑httpd的.conf文件后,重新启动httpd,例如:service httpd restart。您可以创建虚拟主机来解决问题:

<VirtualHost 127.0.0.1:80>
   ServerName localhost
   ServerAlias www.localhost
   DocumentRoot /path/to/your/site/root
   RewriteEngine On
   RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>

然后对 SSL 站点执行相同的操作:

<VirtualHost 127.0.0.1:443>
   ServerName localhost
   ServerAlias www.localhost
   DocumentRoot /home/localhost  #path to your localhost root directory
   RewriteEngine On
   RewriteCond %{HTTP_HOST} ^www'.(.+) [NC]      #rewrite all www to non-www
   RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]
   SSLEngine on
   SSLCertificateFile /home/localhost/ssl.cert
   SSLCertificateKeyFile /home/localhost/ssl.key
   SSLCertificateChainFile /home/localhost/ssl.bundle
</VirtualHost>

编辑2:

对于SSL站点,您可以更改/etc/httpd/conf.d/ssl.conf(这可能取决于您的Linux发行版),但同时,没有真正的需要,您可以将指令放在httpd.conf中。