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

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: hagertyforjudge.dev 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:
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.

  • Doesn't WP create an .htaccess file? I am pretty sure it does. Check again. – closetnoc Feb 14 '16 at 5:26
  • Thanks @user3208862, but WP did not create an .htaccess file. I am showing all hidden and system files; I can see an .htaccess file in a different directory; and when I searched my development directory and all subdirectories, it returned a file I had renamed (DOT.htaccess-original), which shows that the search worked, but no actual .htaccess file. And it's not a cached 301 redirect either, as I forced Firefox to "forget" about the site via the history tab. – user3208862 Feb 14 '16 at 5:35
  • Just remove one line from host file. I think there is mystery on windows host file. – Goyllo Feb 14 '16 at 7:39
  • @closetnoc Doesn't WP create an .htaccess file? Yes, it does however it should be noted only if the directory is writable, and you 'MUST' trigger it by setting up permalinks. – Simon Hayter Feb 14 '16 at 12:07
  • @SimonHayter I know nothing about WP. I have not even thought about trying it for general interest. I just know that we have seen enough WP .htaccess files here... ;-) Sum total knowledge on that subject! What I know fits in a sentence. – closetnoc Feb 14 '16 at 16:15

If I visit hagertyforjudge.dev in my browser, I get a 301 redirect to www.hagertyforjudge.dev Can 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.

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].

| improve this answer | |
  • Thank you! I was not aware that WordPress could set the HTTP response header. Now that you have told me that it does, a little research reveals that there is a function, status_header, that does just that. And when I looked at the file \wp-includes\functions.php, I found a bunch of code dedicated to setting the HTTP header, including a table of all the possible response codes, from which WP chooses the one it will set. Mystery solved, and I learned more about how WordPress and Apache work together. Thanks again! – user3208862 Feb 14 '16 at 18:14
  • WordPress is written in PHP, so ultimately WordPress must call PHP's header() function to actually set the HTTP response header (which I believe is what the status_header() function does). – MrWhite Feb 14 '16 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.