The Art of Redirecting & Rewriting in .Htaccess

Home/Integrated Digital Marketing/The Art of Redirecting & Rewriting in .Htaccess
The Art of Redirecting & Rewriting in .Htaccess

The Art of Redirecting & Rewriting in .Htaccess

Redirecting can be done in many ways in Web side as well as server side. It is easily to implement the redirects in a simple manner which we call as .htaccess it can be used the Redirect & the Redirect Match directives.

Redirecting & Rewriting in .Htaccess

There are many tools which provide you the details about the  .htaccess and the solutions of the trouble shooting of it. You can create a simple  .htaccess file Notepad, Ultra Edit and Microsoft WordPad

There are more advanced methods for using  .htaccess directives known as rewrite rules that we usually use in the server end & Website end as well using the Apache module mode, which is commonly known as mod_rewite

Many clients of mine asked me for always having a trailing slash at the end of their urls. Looks like it’s great for SEO. The following snippet will always add a trailing slash to your site urls.

 RewriteCond %{REQUEST_URI} /+[^.]+$
 RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

Mainly to write a rule you must know the patterns matching the expressions. Below mentioned are some of the important characters & how to rewrite the engines

Here are some of the most important special characters and how the rewrite engine interprets them:

  • means 0 or more occurrences of the immediately preceding character.
  • + means 1 or more occurrences of the immediately preceding character.
  • ? means 0 or 1 occurrence of the immediately preceding character.
  • ^ means the beginning of the string.
  • $ means the end of the string.
  • . means any character (i.e., it acts as a wildcard).
  • “escapes” the character that follows; for example, . means the dot is not meant to be a wildcard, but an actual character.
  • ^ inside square bracket
  • s ([]) means not; for example, [^/] means not slash.?It is incredibly easy to make errors in regular expressions. Some of the common gotchas that ?lead to unintentional substring matches include:
  • Using .* when you should be using .+, since .* can match on nothing.
  • Not “escaping” with a backslash a special character that you don’t want interpreted, as when you specify . instead of . and you really meant the dot character rather than any character (thus, default.htm would match on defaulthtm, and default.htm would match only on default.htm).
  • Omitting ^ or $ on the assumption that the start or end is implied (thus, default.htm would match on mydefault.html, whereas ^default.htm$ would match only on default.htm).
  • Using “greedy” expressions that will match on all occurrences rather than stopping at the first occurrence. ?The easiest way to illustrate what we mean by greedy is to provide an example: ?    RewriteRule ^(.*)/?index.html$ /$1/ [L,R=301]
  • This will redirect requests for http://www.yourdomain.com/blah/index.html to http://www .yourdomain.com/blah//. This is probably not what was intended. Why did this happen? Because .* will capture the slash character within it before the /? gets to see it. Thankfully, there’s an easy fix: simply use [^] or .*? instead of .* to do your matching. For example, use ^(.*?)/? instead of ^(.*)/? or [^/]+/[^/] instead of .*/.*. ?So, to correct the preceding rule, you could use the following: ?    RewriteRule ^(.*?)/?index.html$ /$1/ [L,R=301]
  • When wouldn’t you use the following? ?    RewriteRule ^([^/]*)/?index.html$ /$1/ [L,R=301]

Recently I came across the htaccess that Prevents WordPress from hacking. Which did Fernando Tellado write.

Do have a look at the same where it provides more useful information  Evita hackeos en WordPress desde .htaccess

zp8497586rq

No Comments on This Post

Leave a Reply

Your email address will not be published. Required fields are marked *

Digital & Online Marketing Consultant

Passionate Digital Marketing consultant connecting startups and SME to their target audiences.

Microsoft Advertising Certified Professional

Digital & Online Marketing Consultant