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:
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:
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.
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
RewriteCond $1 !^(index\.php|favicon.ico|public|docs|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
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:
Will be translated to:
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!