.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 says 1.2.3.4 that has sub folder of my wordpress application says ABC in the /var/www/html/. DNS has been setup so that www.mydomain.com and mydomain.com point to IP 1.2.3.4.

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

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

When I put www.mydomain.com the browser automatically change the URL to mydomain.com.

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

Related posts

2 comments

  1. 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 http://www.example.com. 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 “http://www.example.com“.
  2. 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 in wp-config.php as below:

    define('WP_HOME','http://www.myurl.com');
    define('WP_SITEURL','http://www.myurl.com');
    

    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 http://www.myurl.com/wp-login.php?redirect_to=http%3A%2F%2Fwww.myurl.com%2Fthefolder%2Fwp-admin%2F&reauth=1

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

Comments are closed.