DEV Community

loading...
Cover image for How to Force www or non-www in htaccess File

How to Force www or non-www in htaccess File

midexigner profile image Muhammad Idrees Updated on ・1 min read
  • Step1: Log into your cPanel.
  • Step2: Find the Files category and click on the File Manager icon.
  • Step3: Click Settings at the top right corner.
  • Step4: Choose the document root for the domain you are working with and be sure the checkbox next to Show Hidden Files is checked. Click the Save button.
  • Step5: Look for the .htaccess file and right-click on it. This brings up a menu. Find and click on the Edit option
  • Step6: 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]
Enter fullscreen mode Exit fullscreen mode
  • Step7: Now, when you type in your domain name with either www in front or not, it should display as you have set it in the .htaccess file.

Discussion

pic
Editor guide
Collapse
aslasn profile image
Ande

lmao i thought you asked for help. so I came to help. and ...

Collapse
midexigner profile image
Muhammad Idrees Author

what you need for help