首页 > 解决方案 > 我的域永久移动了 301,最终加载或不加载我的 Apache 服务器

问题描述

我有一个 Apache 服务器,其根网站目录位于/var/www/html/。我在 GoDaddy 上购买了一个包含 @A 记录的域,该记录指向我的服务器所在的家庭 IP 地址。在服务器端,我的网站目录中有一个私人文件夹,使用用户名和密码正确保护,正如您在我的服务器配置中看到的那样。我注意到,当我使用 Chrome 访问我的域时,有时它会立即加载,而其他时候(根据 Google Chrome 开发者网络控制台)我会不断从我的服务器获得“301 永久移动”状态代码。有什么我忽略的吗?

我的网站 conf 文件(替换为 example.com 以获取临时隐私):

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName www.example.com
    ServerAlias example.com
    ServerAdmin example@example.com
    DocumentRoot /var/www/html
    ErrorDocument 404 /404.html 

    <Directory "/var/www/html/private">
        AuthType Basic
        AuthName "Restricted Content"
        AuthUserFile /etc/apache2/.htpasswd
        Require valid-user
    </Directory>
    
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

这是位于我网站根目录的 .htaccess 文件,它可能会有所帮助:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^/(.*)$ http://www.example.com/$1 [L,R=301]

标签: apache.htaccess

解决方案


我终于找到了一个可行的解决方案。这不是服务器的错,而是我设置 GoDaddy 的方式。在您的域的 DNS 设置中,让服务器永久重定向到您的公共 IP 地址。确保它是掩码重定向,以便您的域保留在 Web 地址栏中。


推荐阅读