2

I am trying to write a custom redirect rule to redirect a subdomain name to the matching page.

Example: wppage.example.comexample.com/wppage

I am using the following code in my functions.php:

function custom_rewrite_basic() {
    $rewrite = add_rewrite_rule('([^/]+)/?$', 'index.php?pagename=$matches[1]', 'top');
}

add_action('init', 'custom_rewrite_basic');

I've also added wildcard forwarding for all subdomains from my DNS and verified that my .htaccess has the default WordPress mod_rewrite rules.

Not sure why, but I keep getting redirected to my homepage.

What am I doing wrong?

1 Answer 1

2

It's better not to use these sort of subdomain redirect rules within WordPress:

  1. WordPress may not load properly when you access it using the wrong domain name or sub-domain, as a typical WordPress installation is set to run for a particular domain or subdomain.

  2. Even if you are able to make it work, before every redirect WordPress will first load itself and then do the redirect and then load again when the redirected URL reaches the server. That's a slow process.

Instead do it in .htaccess. Like the following:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# custom rules for subdomain matching
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule . http://example.com/%1 [R=301,L]

# default WordPress rules
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

This will redirect all sub.example.com to example.com/sub.

Note: The above CODE will also redirect www.example.com to example.com/www. If you don't want that, then modify the above # custom rules for subdomain matching section to the following:

# to redirect www.example.com/* to example.com/*
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ "http://example.com/$1" [R=301,L]
# custom rules for subdomain matching
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$ [NC]
RewriteRule . http://example.com/%1 [R=301,L]

Read this doc. to learn more about apache mod_rewrite.

Also, make sure mod_rewrite is enabled for .htaccess in your server.

4
  • Thanks a lot for this! But, on point 2 of your logic I know that wordpress always performs a redirect because all routes go through index.php and then use WP_Rewrite to point you to the correct path based on your url. The plus side of using wordpress redirecting is it's programmable. I'll mark this as the correct answer although I think going through WP is more logical. Thank you all the same :-) Commented Feb 27, 2017 at 6:24
  • 1
    No, actually WordPress doesn't perform any redirect only to route everything through index.php. It's the web server that sends everything to index.php & that's not a redirect, that's called an internal rewrite. Read here: wordpress.stackexchange.com/a/256961/110572. Also, WP_Rewrite mainly maps URL structure with data & template, it doesn't have to do any redirect just to serve different URL(s) through index.php. Redirect specifically means a round trip from the server to the user's browser and then to the server again. Your are confusing between redirect & internal rewrites.
    – Fayaz
    Commented Feb 27, 2017 at 8:18
  • Also, it's not always illogical to do redirects with WordPress either, that's not what I said. However, doing redirects with the web server (like with .htaccess) is always faster. In fact you can do a combination, use the Web server redirects for simple rules like these (especially the ones that can be done with a simple pattern), and for other redirects that are hard to maintain with simple patterns like these (say you have a manual list of urls mapping to some other urls), use WordPress. That's fine too.
    – Fayaz
    Commented Feb 27, 2017 at 8:26
  • Thanks for clearing that up. Yes, I confused redirects with rewrites. I think my use case is geared more towards a rewrite rule as I will be rendering these programatically later on. Commented Feb 27, 2017 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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