问题原文
原帖链接 · bhagerty · CC BY-SA 3.0
I am setting up a development environment with XAMPP on Windows. Apache version is 2.4.17.
My development url is www.hagertyforjudge.dev, also accessible at hagertyforjudge.dev. I am trying to solve a mystery about getting a 301 redirect when I don't think I've set up anything on the server side that would call for it.
Here are the relevant lines from httpd-vhosts.conf:
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "H:/xampp2015/htdocs/hfj.dev"
ServerName www.hagertyforjudge.dev
ServerAlias hagertyforjudge.dev
<Directory "H:/xampp2015/htdocs/hfj.dev">
AllowOverride All
Require all Granted
</Directory>
</VirtualHost>
I have not changed the default setting for UseCanonicalName, so it should be off (though it shouldn't matter, I don't think, for my issue).
There is no .htaccess file in the site directory.
Here are the lines from my hosts file that enable me to access this fake site in my browser:
127.0.0.1 hagertyforjudge.dev
127.0.0.1 www.hagertyforjudge.dev
I can access the site fine with any browser (both at hagertyforjudge.dev and at www.hagertyforjudge.dev), and in fact I was able to set up WordPress. I did so by visiting www.hagertyforjudge.dev. That URL got baked into the WordPress database (as 'site_url' and 'home' in the 'options' table). So far so good.
Here's what I don't understand: If I visit hagertyforjudge.dev in my browser, I get a 301 redirect to www.hagertyforjudge.dev. Here is all of the request and response information, according to FireFox's dev tools (Chrome's dev tools give the same info):
Request URL: http://hagertyforjudge.dev/
Requet method: GET
Remote address: 127.0.0.1:80
Status Code: 301 Moved Permanently
Version: HTTP/1.1
Request headers:
Host: hagertyforjudge.dev
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:44.0) Gecko/20100101 Firefox/44.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: wordpress_test_cookie=WP+Cookie+check; wordpress_logged_in_8b3181539def2da5a4654e12bb9d4938=bhagerty%7C1455596780%7C662a3e414d01c0b773a41027df5f3da8; wp-settings-time-1=1455423988
Connection: keep-alive
Response headers:
Connection: Keep-Alive
Content-Length: 0
Content-Type: text/html; charset=UTF-8
Date: Sun, 14 Feb 2016 04:34:24 GMT
Keep-Alive: timeout=5, max=100
Location: http://www.hagertyforjudge.dev/
Server: Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.6.14
X-Pingback: http://www.hagertyforjudge.dev/xmlrpc.php
X-Powered-By: PHP/5.6.14
I did not tell Apache to do a 301 redirect. (Per this guy's discussion of redirection, the ServerAlias setting in the VirtualHost file is internal to Apache and does not generate a 301 or 302 redirect). I don't have an .htaccess file that would cause a 301 redirect.
So How can a 301 redirect be happening? It has to come from Apache, right? But I don't think I've set up a 301 redirect in Apache. I really appreciate any insight you might have.
社区高分解答
原帖链接 · MrWhite · CC BY-SA 4.0
If I visit
hagertyforjudge.devin my browser, I get a 301 redirect towww.hagertyforjudge.devCan WordPress push a 301 redirect?
Yes, it's quite probable that WordPress is issuing this canonical redirect to the host that is "baked into the WordPress database". I believe this is the default behaviour.
If WordPress is issuing the redirect (and the correct WordPress functions are being called to trigger the redirect) then this should be made known in the X-Redirect-By HTTP response header on the redirect response itself. For example:
X-Redirect-By: WordPress
WordPress does not need a .htaccess file (or "Apache config") to "work". However, you miss out on some "pretty URL" functionality if it's not used. The www canonical redirect (in .htaccess) is not part of a default WordPress install, however, many people add it since it is more efficient to do it this way (early in the request), rather than let WordPress handle it.
So How can a 301 redirect be happening? It has to come from Apache, right?
WordPress (ie. PHP) sets the appropriate HTTP response headers and Apache sends the response to the client. A 301 redirect is nothing more than an "HTTP status code" and a Location: response header that is then processed by the browser.
it's not a cached 301 redirect either, as I forced Firefox to "forget" about the site via the history tab.
Just to clarify, the browser history is not the browser cache. To clear the cache go to:
Options > Advanced > Network > Cached Web Content > [Clear Now].
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
