How to force www or non-www in your .htaccess file in cPanel


Cheap reseller Hosting


Force www or non-www in your .htaccess file in cPanel

Step 1:  Log into your cPanel.

Step 2: Find the Files category and click on the File Manager icon.
  

Fail_Manager

Step 3 : A popup box will appear. For the primary domain, click on the Web Root radio button. For addon domains, click on the dropdown and find your desired addon domain name. Be sure the checkbox next to Show Hidden Files is checked.

Step 4 : Click the Go button to enter the File Manager.

Step 5 : You should now be in the root folder of the domain you chose. Look for the .htaccess file and right click on it. This brings up a menu.

Step 6 : Find and click on the Edit option. If you get a popup box, simply find and click the Edit button in the lower right corner to continue to the editor.

 Step 7 : You are now in the text editor. Place either selection of code in the file and click on the Save Changes button at the top right corner of the screen. Be sure to replace ‘example.com‘ with your actual domain name.

NOTE: Do not place both selections of code in the file as it will cause an error.

    #Force www:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]

    #Force non-www:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

 Step 8 : Once you insert the code, click on the Save Changes button in the upper right corner to save the code change. You can now type your domain name either with or without the www and it will display in the address bar in the format you have set the code for.

 

 


Cheap web Hosting


Redirect domain to subdirectory without changing URL


Cheap reseller Hosting


The simple way to redirect your primary domain to a sub folder without change the URL

Redirect_Domain

A)  Redirect and keep everything after the URL

Show all of the same content on one URL as you would another.

Example, if you just changed your domain to DomainB.com, but you still have plenty of visitors coming to DomainA.com, you would use this to show them all of the existing content that is located on the new domain, without the need to update both websites.

You would modify your .htaccess file for the domain that your users will go to, and insert these lines of code:

RewriteCond %{HTTP_HOST} ^DomainA.com
RewriteRule ^(.*) http://DomainB.com/$1 [P]

If you are using the file manager in cPanel, be sure that you have the option to show hidden files selected.

What does the above redirect do?

Adding this line into your .htaccess file, you will be able to go to DomainA.com/YourPage and it will show the content from DomainB.com/YourPage

B) Redirect a domain to a specific url

If you want visitors to go to DomainA.com with a specific page in mind when doing so, you may use this code:

RewriteCond %{HTTP_HOST} ^DomainA.com
RewriteRule ^(.*) http://DomainB.com/PathToPageHere [P]

Example, you had an external blog such as one on blogspot.com or maybe a shopping cart on etsy.com that you want people to visit your domain without fully hosting the domain there. Now, visitors can access your site using your domain, but see the content of an external URL.

C)  Re-directing an IP address

The following code shows how this can be done in the .htaccess file.

# Redirect all IP address (replace the ## with the IP address numerals) to same http://domain_name.com
RewriteCond %{HTTP_HOST} ^##\.##\.##\.##
RewriteRule (.*) http://domain_name.com/$1 [R=301,L]
Stipulations / Please read

Note: If using the first option, search engines such as Google or Bing will see multiple sites with the same content and can cause ranking drops in one, or both of the sites. From an SEO standpoint, your best option is to create 301 redirects instead.

 


Cheap web Hosting