Last Updated on April 20, 2021 by Amit

htaccess copy & paste rules

In this post you will find 5 mod-rewrite rules that you can just copy and paste to your htaccess file.

htaccess copy paste rules with code examples.
All the rules provided here are tested and works perfectly fine in an htaccess file. If you are one of those guys who loves to copy and paste code , then this post is for you. I have provided 5 RewriteRules here that you will need most often in an htaccess file.

Table of contents

  1. WWW domain to non-www
  2. Non-www domain to WWW
  3. http URLs to https
  4. Remove php extension
  5. Disable image hotlinking
  6. Mod-rewrite custom error document

Www to non-www redirection rule

If your domain is available with two versions www and www then it’s important that you either redirect your non-www to www or the www version to non-www . If you don’t do this and your domain has two different URLs pointing to the same location , it can impact your SEO. So here you have two choices :
Non-www domain to www :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^.*$ https://www.domain.com%{REQUEST_URI} [NC,L,R]

or www domain to non-www :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.com$ [NC]
RewriteRule ^.*$ https://domain.com%{REQUEST_URI} [NC,L,R]

You can use one the rules provided above to either redirect to non-www or www. Just copy the rule above and paste it to your htaccess file. Remember to put this at the top of your htaccess file otherwise your other rules may override it.

SSL redirection rule

You need this rule to redirect your httpURLs to https . After enabling SSL certificate on your website, your next step is redirect your site visitors to a secure connection if they try to visit your site using http scheme.
So here is the rule you can use for this type of URL redirection.

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^.*$ https://www.yoursite.com%{REQUEST_URI} [NC,L,R]

The rule will not work if you are behind a proxy server or you are using Cloud flare because Cloudflare uses a different variable to detect ssl, if your site is on cloudflare , then you can use the following instead :

RewriteEngine on
RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
RewriteRule ^ https://exemple.com%{REQUEST_URI} [NE,R,L]

Remove .php extension from URLs

To make your php URLs clean and shorter you can remove their extension. Here is the rule you need to remove .php extension from your URLs :

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+).php [NC]
RewriteRule ^.+$ /%1? [L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

To remove any other file extensions , you can just replace the php with your extension in the code above.

Disable hotlinking of image files

If other websites or external sources are using your site images then its important that you take some actions against them . Hotlinking of image files consumes your server bandwidth and resources. The quick action you can take to disable hotlinking of image files is an htaccess rule that will check the http referer . If referer is not your own domain then forbid the image requests.
You can use the following htaccess code to achieve this :

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?(www.)?yoursite.com [NC]
RewriteRule (jpg|gif|png)$ - [F]

The rule above checks the referer string , if it doesn’t match the pattern , then a request for images ending with .png , .gif, .jpg gets a forbidden error. This can help save your server resources like the uses of bandwidth.

A custom error document with RewriteRule

Its really simple to create your own custom error document with RewriteRule. If you do not want your 404 requests to go to a default 404 page you can implement your own 404 error handler. For this create a php in your directory . Name it as 404.php this file will be used to serve 404 requests on your server.
And then add the following rule to your htaccess file :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.+$ /404.php [L]

The rule will serve 404.php file for all non-existent requests. If you are using WordPress rules then this might not work as WordPress has its own RewriteRule that forwards all requests to a single page.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.