1

I am using bluehost and a single site wordpress site.

I want when someone types in bob.example.com to show the Wordpress site at example.com, but still use the subdomain address (bob.example.com). I have a piece of PHP in Wordpress that recognizes the subdomain and delivers contact information based on that.

I have tried using this RewriteRule in my .htaccess file and while I feel a step closer, it is just giving new errors:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule ^(.*)$ http://example.com [P]

When using wildcard redirects through bluehost, I get Bad Request and an additional 400 Bad Request error when trying to find the errordocument

Without using wildcard redirects, it does not seem to get to the point where the browser utilizes the .htaccess file in my example.com directory.

What do I need to put into my .htaccess file to get this to work how I want it to?

1 Answer 1

0

You shouldn't need to add anything to .htaccess. Assuming your subdomain points to the same place on the filesystem as the main domain then you need to configure WP to accept requests to both hostnames (to prevent a canonical redirect - which is what I assume you were experiencing?).

In other words, instead of hardcoding the domain name in WordPress (WP_SITEURL and WP_HOME), you need to make this dynamic based on the requested hostname.

For example, in wp-config.php set the following:

define('WP_SITEURL','https://'.$_SERVER['HTTP_HOST']);
define('WP_HOME','https://'.$_SERVER['HTTP_HOST']);

RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule ^(.*)$ http://example.com [P]

This is attempting to "proxy" the request from the subdomain to your main domain - using your server as a reverse proxy. This requires access to your server config to configure (properly).

Your Answer

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

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