Most common .htaccess rules

Home > Guides

It is difficult to keep track of all the different htaccess directives and what to use when. With one wrong update to htaccess file, you can do a lot of harm to your website and your online business. In this article, i have listed the most common htaccess uses that we encounter and it will serve you as a quick cheat sheet whenever you realize you need to touch your precious .htaccess file and are clueless of what kind of rule need to be written in the file to achieve what you want.


3 simple steps to remove index.php from CodeIgniter so that you can have clean URLs.


Step 1 -  Open the file config.php located in application/config path.  Find and Replace the below code in config.php  file.


//  Find the below code

$config['index_page'] = "index.php"

//  Remove index.php

$config['index_page'] = ""


Step 2 -  Write below code in .htaccess file

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]


Step 3 - 


<Directory "/var/www/html">
    AllowOverride All


Caching static Assets


 # 15 Days for most static assets

<filesMatch ".(css|jpg|jpeg|png|gif|js|ico|svg)$">

Header set Cache-Control "max-age=1296000, public"



301 Redirect HTTP to HTTPS

#After FollowSymLinks and Engine On

Options +FollowSymLinks

RewriteEngine On


# Rewrite to https:// protocol

     RewriteCond %{HTTP:X-Forwarded-Proto} !https

    RewriteCond %{HTTPS} off

     RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


Remove WWW from your domain

#Remove WWW

     RewriteEngine on

     RewriteCond %{HTTP_HOST} ^www\.

     RewriteRule ^(.*)$$1 [R=301,L]


301 Redirect an entire directory to homepage

Sometimes it happens that you may have created a set of pages in a directory which are not relevant anymore and you want them removed from your website.


The best way is to Redirect 301 them to your homepage so any “link juice” is transferred to your Domain without being lost, plus you don't get errors of 404.


RewriteRule ^folder_name/(.*)$ [R=301,NC,L]


For example,  ->


To keep the URI and append it to the redirected path

For example,  ->


RewriteRule ^folder_name/(.*)$ /$1 [R=301,NC,L]


301 Redirect a part of the URL segment

If you have changed the names of one of your URI segments, and want to match and redirect appropriately, use the following:


For example: you changed the directory from



Now you want all the URLs that contain new-york to be appropriate 301 redirected to the new “nyc” segment.


You would do the following:


Redirect 301 /usa/new-york/ /usa/nyc/


No matter how many other segments come after the city name, they will all be appropriately redirected now.


Example redirections that will work: → → →


301 Redirect URLs with parameters → →


RewriteCond %{REQUEST_URI}  ^$

RewriteCond %{QUERY_STRING} "color=blu" [NC]

RewriteRule (.*) [R=301,L

Comment Form is loading comments...

Table of contents