URL Redirecting rule htaccess

In this article we will talk about some of htaccess rules that we need sometimes. Don’t worry it’s not hectic just go through this article you will get to know .

Here we given just some lines of code that needed to be placed in your root htaccess.

1. Redirect 404 errors to homepage


For better user experience or for good SEO load custom error page or redirect to homepage. Just put this code to htaccess.
“ErrorDocument 404 /index.php” this rule redirect  404 error to index

ErrorDocument 404 /index.php

 


2. non-www to www


This rule force to  use www in website url
Example :   https://example.com     to   https://www.example.com
I hope you understood.
The first condition verifies that the host value is not empty.
The second verifies that the host value does not start with www.
The third verifies HTTPS (percent HTTPS is either on or off, thus percent HTTPS is either ons or offs, and in the event of an ons, the s is matched). The RewriteRule’s substitute component simply combines the informational pieces to create a complete URL.

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

 


3. Remove index.php and redirect to root


This rule used to remove  index.php or index.html from url and redirect to root
Example :    https://example.com/index.php or index.html     to  https://example.com/

RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,NC,L]

4. redirect a URL to different URL

This rule allow you to redirect a URL to different URL. Redirect directive is a great option to redirect an old url to new url. if any request comes from client on an old URL. it redirect to new url.
Example :    https://example.com/old-url     to  https://example.com/new-url

Redirect https://www.example.com/old-url https://www.example.com/new-url

In above code has given Redirect it consists of two full url. Actually no need to add domain name just add /old-url to /new-url .I have added domain name To make you understand properly. so don’t get confused.

Leave a Reply