Laravel shared server forcefully add www using htaccess

Published on : July 27,2022
Laravel shared server forcefully add www using htaccess

Hi Developer,

This tutorial will show you how to redirect your non-www URLs to www, or vice-versa, using htaccess. Let’s get started.

Web development has a lot of nuances that are learned along the way. One of those is that www and non-www URLs are technically different and that it could hurt your SEO if you keep both around.

For example, the domains https://www.opencodesolution.com and https://opencodesolution.com (note the “www” in one but not the other) are technically different. However, if you click on either, they’ll redirect to the same place, which is a good thing. It helps Google and other search engines to only index one and to not split results.

 

On an Apache server

Most users reading this article will likely be on an Apache server. Here are the steps you’ll need.

Use your host’s file manager or an FTP program to access your .htaccess file. Note that it has a dot preceding it.

If you don’t find, check to see if there’s a htaccess.txt file, which can be renamed to .htaccess if it came with your CMS (edit the file and check its contents to be sure). If there isn’t a htaccess.txt file either, create a new file called .htaccess.

Then insert either of the following lines and modify the code with your domain:

I would use this rule:

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^(.*)$ public/$1 [L]

</IfModule>

 

Congratulations, you have successfully forced www or non-www! 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


Hope it can help you…

Categories : Laravel

Tags : .htaccess

Praful Sangani
Praful Sangani
I'm a passionate full-stack developer with expertise in PHP, Laravel, Angular, React Js, Vue, Node, Javascript, JQuery, Codeigniter, and Bootstrap. I enjoy sharing my knowledge by writing tutorials and providing tips to others in the industry. I prioritize consistency and hard work, and I always aim to improve my skills to keep up with the latest advancements. As the owner of Open Code Solution, I'm committed to providing high-quality services to help clients achieve their business goals.


0 Comments

Leave a comment

We'll never share your email with anyone else. Required fields are marked *

Related Articles