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

667

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 thatwww.mydomain.com andmydomain.com point to IP1.2.3.4.

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

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

When I putwww.mydomain.com the browser automatically change the URL tomydomain.com.

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

110

Answer

Solution:

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 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".
620

Answer

Solution:

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:

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

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

Source

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.