2

We have a bunch of sites that are subdomains. We are changing the structure of these sites so they are a single site instead of multiple WordPress installs. No problem. Many of the subdomains have authority so we want to add 301 redirects from these subdomains to the new corresponding page. However, the new page parent page URLs conflict with the old subdomain structure.

existing subdomain

sub1.example.com

redirect which is now a landing page.

example.com/sub1

There is a conflict because the subdomain folders need to stay in place to perform the redirects.

I am looking for a way to create a redirect in the .htaccess from the subdomains that does not conflict with the new page URL of the same name?

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Garet H.
  • 135
  • 2
  • 12
  • "There is a conflict because the subdomain folders need to stay in place to perform the redirects." - Not sure what "conflict" you are referring to here? The subdomain still needs to be configured in DNS, but it could be changed to point to another part of the filesystem if you needed to (but I'm not sure that you would need to)? – MrWhite Apr 10 '17 at 19:55
  • Thanks for you reply. Sorry I should have been more clear, I am a little shaky in this area. It is generating 404 errors when visiting the new parent page site.com/sub1 as it accessing the subfolder. Essentially I need it so when someone finds sub1.site.com it redirects to the new page called site.com/sub1. But it seems the server sees the subfolder and the new page as being the same and doesn't know what to do. So I need help with a RewriteCond. – Garet H. Apr 10 '17 at 20:06
  • Ah OK, `/sub1` is a URL defined in WP, so it needs to be routed via WP. But if there is a physical directory called `/sub1` (the original target of the subdomain) then WP fails to route the URL (this is because of WP's own mod_rewrite directives). Does the `/sub1` directory on the filesystem _need_ to exist? Can you not change the subdomain so that it simply points to the document root of the main site? Otherwise we can _change_ the WP directives, to allow these URLs to be routed? – MrWhite Apr 10 '17 at 20:17
  • Thanks again. Yes, it is preferred to leave the folders in tact so we can add 301 redirects to the .htaccess files in these folders so we can try to preserve any authority the pages currently have. Although I am definitely open to suggestions. – Garet H. Apr 10 '17 at 20:22

1 Answers1

1

it is preferred to leave the folders in tact so we can add 301 redirects to the .htaccess files in these folders

This is part of the problem. Any .htaccess file in these subdirectories will override the .htaccess file in the parent directory (the main WP directives I assume). (Strictly speaking it's just the mod_rewrite directives that are overridden. Other modules are inherited.)

Instead, try the following:

(NB: This assumes the subdomain points to a corresponding subdirectory off the main domain's document root. It would simpler if you changed the subdomain, so that it also pointed to the document root of the main site. You would then only need a single (simpler) redirect.)

  1. Delete the .htaccess (and index.php) file(s) in the subdirectory and instead add the appropriate directives in the parent .htaccess file.

  2. Near the top of the root .htaccess file (before any existing WP directives):

    RewriteCond %{HTTP_HOST} ^(?:www\.)?(sub1|sub2|sub3)\.example\.com
    RewriteRule ^[^/]+/(.*) http://example.com/%1/$1 [R=302,L]
    

    This would redirect sub1.example.com/<anything> to http://example.com/sub1/<anything>. The www sub-subdomain on the initial request is optional.

    Exactly how you would do this could depend on how many (and what format) subdomains you have. If you have just a handful (you say you have 3) then it would preferable to name these explicitly, as in the above RewriteCond directive.

    It is easier to use a 302 (temporary) redirect whilst testing since they aren't cached by the browser. (Ensure the browser cache is cleared before testing.)

  3. You will need to update the main WordPress directives to include an additional rule/condition for when these bare subdirectories are accessed (eg. http://example.com/sub1/). Since /sub1 is a physical directory, WP will not route the request, because the WordPress front-controller specifically ignores existing files and directories.

    In the main WP .htaccess file, you have the following directives:

    # 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]
    </IfModule>
    # END WordPress
    

    Add the following rule immediately after the above:

    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^(sub1|sub2|sub3)/?$ /index.php [L]
    

    This allows requests for the bare subdirectory (eg. http://example.com/sub1/) to be routed by WordPress.

MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • There are 3 subdomains. Here is an example of what I have for one of the subdomains that I was testing. https://gist.github.com/pagetopixel/6f9e821880abe43568f40ecb6e657ecb – Garet H. Apr 10 '17 at 21:04
  • When I mentioned posting your existing `.htaccess` file, I was referring to the main `.htaccess` file in the document root (for the WordPress multisite). The linked subdomain's `.htaccess` file would seem to only redirect the bare subdomain ie. `sub1.example.com/` and not `sub1.example.com/`. Also, the mod_alias `Redirect` directives you have in that file will not actually do anything... But they are not required if you implement something like I have included above. My answer still stands, have you tried it (I've updated it slightly to allow for the `www` sub-subdomain etc.)? – MrWhite Apr 10 '17 at 22:17
  • Ok I think I see what you mean. One point of clarification it is not running on multisite. I am not sure if that makes a difference but I thought I should mention it. I removed the htaccess from one of the subdomains and added the following to the main htaccess file https://gist.github.com/pagetopixel/959c234d9869cb033b6d9801004feba9 I must still be missing something as the url in the address bar updates but it is still showing the the footer of the subdomain and the message "Page not found The link you followed may be broken, or the page may have been removed." – Garet H. Apr 11 '17 at 00:05
  • "it is not running on multisite." - Sorry, somehow read something that wasn't there! It makes a slight difference to the last stage. There was, however, a slight error with the first redirect. I've corrected this and completely overhauled/restructured my answer. I've tested this on my own server, so the principle at least works OK. – MrWhite Apr 11 '17 at 22:36
  • 1
    Worked like a charm! Thanks for all your help!! – Garet H. Apr 11 '17 at 23:47