Book a Free Call

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

//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

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.

Need rapid website development:

For smaller projects at high speed


  • Adding script tag embed to Gatsby component

    Read More
  • Get more than 100 queries in GraphQL (I felt stupid)

    Read More
  • Animate on Scroll (AOS) into Nuxt js

    Read More
  • WordPress Super-admin hack

    Read More
  • Add content or ACF field under thumbnail/image gallery in Woocommerce

    Read More

Tags


Categories

Is it urgent? Donate to Cork Simon

Got an urgent issue?

Charlie's Cottage, Carrigrohane, Co. Cork, T12 WY9H

(085) 7686677