php - .htaccess mapping www & non-www to folder and maintain www


I am no.htaccess expert and extremely rarely use it. I went through several solutions on Net and SO but no luck still. So I have one IP says1.2.3.4 that has sub folder of my wordpress application says ABC in the/var/www/html/. DNS has been setup so point to IP1.2.3.4.

I have my content in.htaccess file as below and it only The content is displayed as expected except when I it displays Linux home page.

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com$
RewriteCond %{REQUEST_URI} !^/ABC/
RewriteRule (.*) /ABC/$1

When I the browser automatically change the URL

Apart from that, I would also like to have all domain withwww. Means request will be displayed Later I will just in the URL config of my wordpress. Some helps are appreciated.




Switch back to your old .htaccess code. I got it more clear after your last update. You have wordpress files in "public_html/ABC" but need your website url as Let me explain step by step.

  1. Paste your old code in .htaccess.
  2. copy (do not move) the index.php and .htaccess file from the /ABC folder to the "public_html" of your site.
  3. Find one rule in "public_html/index.php" as "require( dirname( FILE ) . '/wp-blog-header.php' );" and replace it with "require( dirname( FILE ) . '/ABC/wp-blog-header.php' );" (do not edit the index.php file in the folder /ABC!!)
  4. change ONLY the "Site Address (URL)" in the general settings (Settings -> General) to "".



So finally I managed to get what I want to be working with below solution. Hopefully this will help somebody out there:

RewriteEngine on
RewriteBase /

# Whatever request coming to the server, add www to it
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# Map the www URL to the subfolder in root directory    
RewriteCond %{HTTP_HOST} www\.myurl\.com$
RewriteCond %{REQUEST_URI} !^/thefolder/
RewriteRule (.*) /thefolder/$1

For Wordpress part, I have put codes inwp-config.php as below:


Thank you. Cheers.

UPDATE I have just realized that after the changes above, I could not able to login to my WP admin page. This is due to wrong redirect. It looks something like

When I remove thethefolder from URL it redirects to expected page. Can somebody advice on this?

People are also looking for solutions to the problem: javascript - Disable Close Modal on Outside Click


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.