Apache虚拟主机非www无法正常运行

时间:2018-06-23 16:25:35

标签: apache centos centos7 virtual-hosts

我正在CentOS 7盒子上设置虚拟主机文件,但是我无法让域正确解析。

这是我当前的/etc/httpd/conf.d/vhost.conf文件的样子

NameVirtualHost *:80

<VirtualHost *:80>
   ServerAdmin webmaster@domain.com
   ServerName www.domain.com
   ServerAlias domain.com
   DocumentRoot /var/www/html/domain.com/public_html/
   ErrorLog /var/log/httpd/error.log
   CustomLog /var/log/httpd/access.log combined

   RewriteEngine on
   RewriteCond %{SERVER_NAME} =www.domain.com [OR]
   RewriteCond %{SERVER_NAME} =domain.com
   RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

似乎正确的重定向正在发生。例如:

domain.com重定向到https://www.domain.com www工作正常

但是

https://domain.com无法正常工作 http://domain.com无法正常工作

实际上,如果我删除了设置的重定向,domain.com根本无法正常工作,因此ServerAlias好像坏了吗?

我想知道是否需要其他重定向,或者是否缺少其他步骤?

此外,不要介意http和域名之间的空格。 StackOverflow让我用这种方式格式化。

2 个答案:

答案 0 :(得分:0)

如前所述,对任何ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL); // Include config file require_once 'functions/db_connect.php'; // Define variables and initialize with empty values $username = $password = ""; $username_err = $password_err = ""; // Processing form data when form is submitted if($_SERVER["REQUEST_METHOD"] == "POST"){ // Check if username is empty if(empty(trim($_POST["username"]))){ $username_err = 'Please enter username.'; } else{ $username = trim($_POST["username"]); } // Check if password is empty if(empty(trim($_POST['password']))){ $password_err = 'Please enter your password.'; } else{ $password = trim($_POST['password']); } // Validate credentials if(empty($username_err) && empty($password_err)){ // Prepare a select statement $sql = "SELECT username, password FROM users WHERE username = ?"; if($stmt = mysqli_prepare($link, $sql)){ // Bind variables to the prepared statement as parameters mysqli_stmt_bind_param($stmt, "s", $param_username); // Set parameters $param_username = $username; // Attempt to execute the prepared statement if(mysqli_stmt_execute($stmt)){ // Store result mysqli_stmt_store_result($stmt); // Check if username exists, if yes then verify password if(mysqli_stmt_num_rows($stmt) == 1){ // Bind result variables mysqli_stmt_bind_result($stmt, $username, $hashed_password); if(mysqli_stmt_fetch($stmt)){ if(password_verify($password, $hashed_password)){ /* Password is correct, so start a new session and save the username to the session */ session_start(); $_SESSION['username'] = $username; header("location: home.php"); } else{ // Display an error message if password is not valid $password_err = 'The password you entered was not valid.'; } } } else{ // Display an error message if username doesn't exist $username_err = 'No account found with that username.'; //echo 'No account found with that name!'; } } else{ echo "Oops! Something went wrong. Please try again later."; } } // Close statement mysqli_stmt_close($stmt); } // Close connection mysqli_close($link); 的请求都将失效。正常情况下,端口80上只有https。该端口上有VirtualHost指令,对吗?

用于重定向。它说:如果您要求Listenhttp://www.example.com,请重定向到http://example.com。从本质上讲,您是在强迫用户始终使用https,在那里没有问题。但是端口443上没有https://<WHAT THE USER ASKED FOR>,因此没有响应。

所以:

VirtualHost
  • 在*:443 VH中,您将必须配置证书和SSL。
  • 您的证书必须同时对 www.example.com和example.com有效,以避免浏览器投诉。
  • 要小心,在conf.d下可能包含一个ssl.conf包含的文件,它定义了其中的一些内容。确保只设置一次,以免造成混淆。
  • 无需在*:80 VH中定义Listen *:80 <VirtualHost *:80> ServerName www.example.com ServerAlias example.com ErrorLog /var/log/httpd/80_error.log CustomLog /var/log/httpd/80_access.log combined RewriteEngine on RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L] </VirtualHost> Listen *:443 <VirtualHost *:443> ServerName www.example.com # in case users do directly to https ServerAlias example.com DocumentRoot /var/www/html/domain.com/public_html/ DocumentIndex index.html ErrorLog /var/log/httpd/443_error.log CustomLog /var/log/httpd/443_access.log combined # SSL CONFIGURATIONS, TODO! </VirtualHost> ,因为它仅重定向并且不向客户端响应内容。

玩得开心!

答案 1 :(得分:0)

我解决了这个问题。我将本地主机文件配置为指向旧的过期IP地址……

domain.com *bad ip address*

我很尴尬。我一定在几个月前就设置好了,忘了。