htaccess for Friendly URL on Web

Have you ever wonder how to change your website URL to be more friendly and pretty one? Like for example, you can see at this site, I use URL like this:

http://www.hernantas.com/2016/05/page-name

Rather than

http://www.hernantas.com/?p=123

I’m sure the first one is friendlier and prettier than the second one.

But the question is, how do I do that? Not for the WordPress one, but for general one like the website you created by yourself.

Normally, when you write URL like this:

http://www.hernantas.com/subdir/subdir2

Your server will access subdirectory subdir/subdir2 from your root server directory. So if you want to use friendly URL, you must write index.php file in each directory you can handle. This isn’t possible in practical use and inconvenient. With .htaccess or server configuration, you don’t need to write it in each subdirectory instead, just write one in the root directory and it will handle all of the user URLs.

There is couple way to do this, but I will use the .htaccess one since it simple and don’t modify server configuration.

Requirements

First, you must have a Web server. (Duh)

Not joking. I have been asked by people who think that website exists just with HTML, CSS, image, and JS. Well, that actually quite true, though. Since the server-side script is just generating that type of file so it can feel like dynamic site rather than static.

I’m sure you already know this but make sure that you have a Web server and it is running correctly and smoothly. And make sure your Web server has to allow override or .htaccess will be ignored. You can see how to do that on apache site.

So, How to do That?

First, open your website directory. In the website directory, create a new file called .htaccess. Edit that file and copy the script bellow to your new .htaccess file.

The first line of the script above is to enable rewriting configuration file. We need to do this since we want to rewrite rule on server configuration so we can redirect any requested URL to the single file.

The second line one is the condition of the rule we will make. It will rewrite the URL except for the file or directory is written in the condition.

The third one will rewrite server rule that any requested URI will be handled by index.php file. With this configuration override, URL like this:

http://www.hernantas.com/subdir/subdir2

Will be translated to:

http://www.hernantas.com/index.php/subdir/subdir2

But don’t worry, it won’t change anything on the user browser. It will only affect the server. I’m sure it is simple enough and don’t really need too much works.

That it for now, if you have any further question or suggestion you can write it on the comment bellow. Thank for reading!

Want more like this one?

How to Run .phar Files Couple days ago, when I working on a web project for some developer company. They ask me if I can tell them how to create API documentation like the o...
Twitter should stay at 140 character limit I found out today that twitter is planning to extend it 140 characters limit. It seems that a lot of people are supporting the changes and of course, ...
Avoiding SQL Injection in PHP Recently, I'm reviewing some web project that made using PHP. It nice website, responsive and can support both PC and mobile display. But, it seems th...
Trello is Celebrating 10M Users! Trello is currently celebrating its 10M users. If you don't know or never heard what Trello is, then you should check it on their website. In short Tr...