Most common .htaccess rules

Home > Programming & Technology

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]
</IfModule>

 

Step 3 - 

/etc/httpd/httpd/conf

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

 

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"

</filesMatch>

 

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 ^(.*)$ https://readbeach.com/$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/(.*)$ https://www.exampledomain.com [R=301,NC,L]

 

For example, 

exampledomain.com/testimonials/username  -> exampledomain.com

 

To keep the URI and append it to the redirected path

For example, 

exampledomain.com/testimonials/username  -> exampledomain.com/username

 

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

 

domain.com/usa/new-york/products

To

domain.com/usa/nyc/products

 

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:

 

domain.com/usa/new-york/products → domain.com/usa/nyc/products

domain.com/usa/new-york/games → domain.com/usa/nyc/games

domain.com/usa/new-york/games/new → domain.com/usa/nyc/games/new

 

301 Redirect URLs with parameters

 

domain.com/shirts?color=blu → domain.com/shirts?color=blue

domain.com/shirts?color=blu → domain.com/shirts/blue

 

RewriteCond %{REQUEST_URI}  ^domain.com/shirts$

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

RewriteRule (.*)  domain.com/shirts?color=blue [R=301,L

Comment Form is loading comments...

Table of contents