Rewrite domain to subdirectory without anyone knowing (WordPress / .htaccess) | Double Marvellous
Back to Blog
Development Jul 18, 2024 4 min read

Rewrite domain to subdirectory without anyone knowing (WordPress / .htaccess)

Rewrite domain to subdirectory without anyone knowing (WordPress / .htaccess)

Well this one is a bit of a doozy. Anytime there are any htaccess rules to implement, it always seems that there are 1000+ possible ways to do the same thing, and many don’t work.

Well I have an issue today. I have placed a WordPress install into a subdirectory, and I need the domain to to to that location when the user browses to it. I don’t want to see the subdomain in the URL path.

Let’s say our domain is yourgreatwebsite.com, and we have installed a WP into the folder named yoursubdomain

We want to see this website fully working at yourgreatwebsite.com. When we want to log into the WordPress backend, we can go to yourgreatwebsite.com/yoursubdomain/wp-admin

And that should be it! You may have some sort of insane rat’s maze of https and other redirects going on in the root folder. if you do, I guess you’re up for a bit of trial and error. But this should get you going.

//add this into the htaccess in the root folder (yourgreatwebsite.com)
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?yourgreatwebsite.com$
RewriteCond %{REQUEST_URI} !^/yoursubdomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /yoursubdomain/$1
RewriteCond %{HTTP_HOST} ^(www.)?yourgreatwebsite.com$
RewriteRule ^(/)?$ yoursubdomain/index.php [L]



//add these lines after 'define( 'DB_COLLATE', '' );' in the wp-config file (where you add it is not massively important, aroudn there is fine)
define('WP_HOME','http://yourgreatwebsite.com/');
define('WP_SITEURL','http://yourgreatwebsite.com/yoursubdomain');



//change one line in the htaccess in your wp install - from 'RewriteRule . /index.php [L]' to this: 'RewriteRule . /yoursubdomain/index.php [L]'
# BEGIN WordPress

RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /yoursubdomain/index.php [L]

# END WordPress

Double Marvellous

AI Chatbot

Double Marvellous.