Dead introduction to htaccess

Website Design |

2nd Nov, 2019

Click here to share

Htaccess is a must-have experience by every web developer. This is because with the knowledge you will be able to manipulate all the features of your server especially if you are making use of shared hosting where you won’t have access to the main apache configuration files (that is to say No Root Access).

Initially, I was very scared of htaccess because I never studied regex (regular expression) which is the basics of htaccess, but the phobia came to an end when it became difficult for me to keep going without the knowledge of htaccess and I don’t have anything else to do more than to learn it.

This need of mastering htaccess came as a result of a project I was working on whereby my client is using shared hosting and having learnt Laravel I was very happy and convenient with myself, saying that I don’t need any knowledge of URL rewriting since with Laravel route everything about URL is handled by the files inside the route folder and captured in the controller or sent directly to the appropriate blade file.

For those who don’t know what Laravel is

                Laravel is a PHP framework for making dynamic web pages with fast and easy. You can visit our Laravel section to learn more about Laravel or even read our blog tutorial which has been packaged to make you a perfect Laravel developer in minimal time.

But in this case, it was already a full project so the only thing I have to do is to create some files and ensure that the URL pattern going to the new files follows the standard.

To make the case more complex some of these files have query strings (query strings are parts after the question mark on a URL

<image of a URL with a query string>

And I have to make this URL look like the expected result as shown in the image if you have never known the names all these variables are called inside apache htaccess you will see the job as a nightmare whereas it’s just a cinch when you know what to do.

After going through the project description I have to decline the projects. Guess the next thing I did.

I went for a complete course on regular expression and htaccess.

Along the way I saw a lot of blog posts claiming to tech htaccess very well but quite unfortunate most of them never cared about beginners on regular expression and that made me keep wandering from one blog to another until I was able to get to the root of the expressions which marks my introduction to htaccess and regular expression.

Some of the websites just gave me the solution by writing a series of examples which I can copy and replace my variables but that is never what I wanted.

I wanted it to start like a beginner tutorial where all the tags and characters present in regex and htaccess are explained correctly before giving an example.

After the research and have found what I want I decided to make this post as a reference to me and any other person that might see his or herself in this kind of situation at any point. So that the person can easily pick up and get to project in a very short time frame.

In this lesson, it is assumed that we only specialize in htaccess and don’t care what runs on the server for those havening root access. So if you want to follow the syntax and lesson for those using root access please back off now for that is not covered here




Htaccess is an apache configuration file that controls the Web Server running your website, and it is the most powerful configuration files you will ever come across.

.htaccess can control access/settings for the HyperText Transfer Protocol (HTTP) using Password Protection, 301 Redirects, rewrites, and far more. This is because this configuration file was coded in the earliest days of the web (HTTP), for one of the first Web Servers ever! Eventually, these Web Servers (configured with htaccess) became known as the World Wide Web, and eventually grew into the Internet we use today

Important tags, the syntax for htaccess for beginners

These are the most important things that you must know before you can be able to write a working htaccess file

This is like a character reference thou not covering everything but it will be a quick guide to a beginners




This matches anything that begins with what it precedes. Eg(^a) this matches anything that starts with a.

This also matches the beginning of a match pattern


This matches the end of a match. It also takes into consideration the last pattern before this sign($)

Eg (dollar$) this  matches anything that ends with the world dollar


This is the combination of both start and end. It matches the beginning and the end of a pattern. This also means it matches the entire patter. Eg(^a series of pattern$)

This will match a string with exactly the world (a series of string) the word must be the same otherwise there will be no match


This matches any character eg(^(.)$)

This will match exactly one character not minding what it is.


This will match 1 or more character(^(.)+$).

This means start with any character (dot in this instance) match unlimited numbers of characters (plus in this case) and then stop


The question mark has lots of work to do but in this instance, we will talk about the most common which is telling the match to be either one or zero.

Eg (^.?$) This will match exactly one character or zero. So if you don’t pass any character you are a god to go, if you pass at least one that is good but if it gets more than one character (dot in this case: any character) the match will not fail, that is it will not catch this one.


This is another character that does wonder in matching. This star character matches any of its preceding match zero or more times. Eg(^(match)*$) this will match the world match any amount of time, zero is included. This means that it will match even if the match is not there


Now to apply these in an htaccess file

A URL may look like

Now your task will be to make this URL look like Worldtok/any-blog-title

To do this you have to consider the pattern of the incoming URL and then know the way you want it to look to your users




Inside your htaccess, you have to write this

Rewriteengine on

Rewrite base /

RewriteRule ^blog/([-\w]) /$1 [R=301,L]

This simple example will take us to explain the important reference keywords and characters that are unique to the only apache used in as it is been used in htaccess file

Leave a Reply


Total of 0 Comment