Apache2 和 Wordpress 中不需要的 301 重定向

Apache2 和 Wordpress 中不需要的 301 重定向

我的文件配置如下apache2.conf

DocumentRoot /var/www/html
<Directory / >
        # Options FollowSymLinks
        AllowOverride None
        Require all granted
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
</Directory>

我相信这几乎是 WordPress 网站的样板。如果我使用卷曲请求/index.php它工作得很好,但如果我请求根,http://localhost/它在正文中不会返回任何内容,并且标题如下所示:

> GET / HTTP/1.1
> Host: localhost
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Date: Sun, 30 Jun 2019 18:31:38 GMT
< Server: Apache/2.4.18 (Ubuntu)
< Location: http://ken.net/
< Content-Length: 0
< Content-Type: text/html; charset=UTF-8
< 
* Connection #0 to host localhost left intact

我还是个新手阿帕奇配置,但我看不出有什么特别之处。它几乎是样板……不管你信不信,它曾经工作过(几个月前,但这个项目搁置太久了)。


添加完整apache2.conf

Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100


KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups Off

ErrorLog ${APACHE_LOG_DIR}/error.log
LogLevel warn

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

Include ports.conf

DocumentRoot /var/www/html
<Directory / >
        # Options FollowSymLinks
        AllowOverride None
        Require all granted
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.php$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
</Directory>

AccessFileName .htaccess
<FilesMatch "^\.ht">
        Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf

该目录mods-enabled如下所示:

access_compat.load  authn_file.load  autoindex.load  env.load          mpm_prefork.load  rewrite.load
alias.conf          authz_core.load  deflate.conf    filter.load       negotiation.conf  setenvif.conf
alias.load          authz_host.load  deflate.load    mime.conf         negotiation.load  setenvif.load
auth_basic.load     authz_user.load  dir.conf        mime.load         php7.0.conf       status.conf
authn_core.load     autoindex.conf   dir.load        mpm_prefork.conf  php7.0.load       status.load

现在我知道了./sites-enabled/*.conf主配置中的声明,我已从<DIRECTORY></DIRECTORY>主配置中删除了说明,并在默认站点配置(又名/etc/apache2/sites-enabled/000-default.conf)中将其更改为:

<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

        ServerAdmin [email protected]
        DocumentRoot /var/www/html

        <Directory / >
                # Options FollowSymLinks
                AllowOverride None
                Require all granted
                RewriteEngine On
                RewriteBase /
                RewriteRule ^index\.php$ - [L]
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteRule . /index.php [L]
        </Directory>>

        #LogLevel info ssl:warn

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

        #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

答案1

Wordpress 和类似程序天真地认为只有一个域指向那里。(并且反向代理和后端上的协议是相同的。如果您的 apache 使用 https,并且 wordpress 反向代理到其他东西(其他主机,同一主机的其他服务),您也会遇到同样的问题)。

因此,除非您可以配置 wordpress 以停止重定向(可能是插件或某些 php 修补),否则您只需确保您在 wordpress 中设置的 URL 与用户用于访问您的网站的 URL 相同。

如果你想在测试环境中做一些不同的事情,我建议使用原始生产 URL(在你的情况下http://ken.net, 不是http://本地主机),但在客户端的 hosts 文件中将其 IP 更改为您的测试环境(例如 127.0.0.1 ken.net)。这样测试就很容易了,无需重新配置 URL,也无需根据配置做出不同的行为。

相关内容