Directing subdomain to main domain and keeping the subdomain format with .htaccess

admin2025-04-19  0

I am using bluehost and a single site wordpress site.

I want when someone types in bob.example to show the Wordpress site at example, but still use the subdomain address (bob.example). 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\$
RewriteRule ^(.*)$  [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 directory.

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

I am using bluehost and a single site wordpress site.

I want when someone types in bob.example to show the Wordpress site at example, but still use the subdomain address (bob.example). 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\$
RewriteRule ^(.*)$ http://example [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 directory.

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

Share Improve this question asked Oct 28, 2019 at 4:57 LisaLisa 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 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\$
RewriteRule ^(.*)$ http://example [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).

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745042100a281606.html

最新回复(0)