For the last 3 years I've had 2 copies of my Wordpress website. One at www.domain.com and one at dev.domain.com. I'm trying to add a third today. Seems simple enough! I go through all the simple setup steps and as soon as I enable the site it's 301 directing me to the www site. I don't know how to troubleshoot what is triggering the 301. Is it somewhere in Apache or in Wordpress? All I know is that I'm copying all the configuration for the dev site and changing the subdomain and directory names wherever applicable. Seems like that should be enough. My steps:

  1. Open dev2.domain.com in a browser and see the default Apache2 page
  2. Copy source folder for /dev.domain.com -> /dev2.domain.com
  3. Copy /etc/apache2/sites-available/dev.domain.com.conf -> dev2.domain.com.conf
  4. Execute: sudo a2ensite dev2.domain.com.conf
  5. Execute: sudo service apache2 reload (and/or restart - I've tried both)
  6. Open dev2.domain.com and get 301 redirected to www.domain.com

Is there anyway to definitively figure out what's causing the 301? What's most confusing is that I'm standing up this new site on a subdomain by copying an existing site that's running on a subdomain. I just can't figure out what the difference is between the two.

Obligatory configuration info...

Identical .htaccess in root directory of all 3 sites (www, dev, and dev2):

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# END WordPress
Options +Indexes
IndexOptions -FancyIndexing

sites-available configuration for dev.domain.com...

<VirtualHost *:80>
    ServerName dev.domain.com
    ServerAdmin d@domain.com

    DocumentRoot /srv/dev.domain.com

    <Directory />
        Options All
        AllowOverride All
        Require all granted

    ErrorLog /srv/dev.domain.com/logs/error.log
    CustomLog /srv/dev.domain.com/logs/access.log combined

sites-available configuration for dev2.domain.com...

<VirtualHost *:80>
    ServerName dev2.domain.com
    ServerAdmin d@domain.com

    DocumentRoot /srv/dev2.domain.com

    <Directory />
        Options All
        AllowOverride All
        Require all granted

    ErrorLog /srv/dev2.domain.com/logs/error.log
    CustomLog /srv/dev2.domain.com/logs/access.log combined

DNS configuration in AWS Route53...

domain.com       A  12.34.567.89
dev.domain.com   A  12.34.567.89
dev2.domain.com  A  12.34.567.89
www.domain.com   CNAME  domain.com

I guess the 2 things that I'm most stuck on:

  1. The apache configuration between dev and dev2 seem to be identical other than changing "dev" -> "dev2" wherever applicable

  2. When the site is disabled in apache it does not get redirected. The redirection only happens once the site is enabled. Does this mean it's Wordpress?


Your Apache settings are all accurate; your issue is most likely a result of the domain being saved in the Wordpress database. By default, Wordpress stores the base url in the wp_options table for both the homepage and site url. In addition, it stores the fully qualified domain in all the individual post/page GUIDs in the wp_posts table.

Options to find & replace the domain:

  • quickest: export the mysql database, open the mysql database dump in a text editor and perform global find (for domain.com or dev.domain.com) and replace with (dev2.domain.com), reimport.
  • use a sql query to perform the find and replace all instances of the old domain with new subdomain.

For more background about migrating a Wordpress site from one domain to another, see the section entitled "Changing Your Domain Name and URLs" on the Moving Wordpress Codex page

  • I forgot to add this as one of the steps I used, but the export->find/replace->import is how I duplicated the database. I'll double check to try to make sure I didn't miss anything or screw up some setting in the find and replace. Thanks for the idea! Dec 14 '17 at 16:32
  • Also you may want to check the wp-config.php file, sometimes the baseurl or siteurl is set via php. Dec 14 '17 at 16:35
  • It seems as though performing a simple export -> find/replace -> import doesn't work with my wordpress installation. I used the WP Migrate DB tool to do the find and replace and that worked. Not sure what the difference might be, but thanks for setting me down the right path! Dec 18 '17 at 14:09

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.