.htaccess
file is a configuration file used to override the main server configuration. .htaccess
file placed in a directory and the configuration is applied to that particular directory and all the subdirectories. The most usage of .htaccess
file is Rewriting URLs, Blocking, SSL, Customized error responses, Directory listing, Cache-Control, etc.
You can define the conditional URL rewrite rules using RewriteCond and RewriteRule in HTACCESS. This HTACCESS tutorial will show you the most used URL redirection process through the .htaccess file. At first, you’ll see how to redirect non-www URLs to www and the second one will show you how to redirect HTTP to HTTPS using a .htaccess file.
Create a file with .htaccess
extension in the root directory of the website domain. It will be a simple text file and you can edit this file with any text editor.
As per the Search Engine prospect, non-www and www URLs are different and it could affect website SEO. For example, http://codexworld.com
and http://www.codexworld.com
are technically different in Search Engines. When a Search Engine fetches the same content from different URLs, it is treated as duplicate content. So, it will be a good idea if you redirect all requests (non-www) to the same URL format (www). Using the .htaccess file, you can easily redirect non-www to www URL.
Add the following code in the root .htaccess
file to redirect the non-www to www URL.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
If the secure certificate (SSL) was installed on your website, you can redirect visitors to the secure web version. Sometimes SSL encrypted connection is necessary for your website. This means you would like to force website visitors to use SSL by automatically redirecting HTTP to HTTPS version of the URL.
Add the following code in the root .htaccess file to redirect the HTTP to HTTPS URL.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
It’s always been a good idea to maintain the same URL format for all the requests. To fulfill this purpose, redirect non-www and HTTP requests to HTTPS and www formatted URLs.
The following HTACCESS code snippet redirects all HTTP & non-www requests to HTTPS & www URL.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Here, we have discussed the most used rewrite rules and redirect URLs using HTACCESS. If you need any help regarding the .htaccess
file, feel free to post your comment below.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request
You can do it with one redirect..
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [NC,L,R=301]
Just replace with Your domain.
I have applied it. work well. but just for the home page. Thanks
What about other pages??
Perfect! Old article but still works great 5 years later!
Hi, This is working fine but my other addons domains are affected they are also forwarding with my primary domain even i have not installed the SSL certificate for others…. waiting for positive response
Tanks it works great! But after i use it, i can’t remove index.php . I’m using RewriteRule ^(.*)$ index.php/$1 [L]. Can you help me. Tank you, all the best !
Great Help
I m using codeigniter. It works for me. I was used at my site wiki-mobiles. Thanks
Thanks it works for me.
Thanks. its working
Your article is very nice to new PHP developers, i am also new in PHP development field and your post very helpful for me to understand about .htaccess. You have written very simple English language so thanks for sharing.
nice, tnx
Good Job. Its very useful me and all
Nice and clean. This is what I’ve expected.