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

时间:2014-04-05 09:30:22

标签: php apache

我需要通过以下网址访问网络应用:

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”协议下提供?

1 个答案:

答案 0 :(得分:0)

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 Directive:

  

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

编辑1:

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

<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