Should you htaccess or server.config file on apache server?

Should you use htaccess or server.config file?



This is a common question among many apache mod-rewrite users especially among newbies as they are curious about it.
If you search this topic on google you will get hundreds of top results where experts advice you not to use htaccess.
One true answer to the question can be found on official apache mod-rewrite manual https://httpd.apache.org/docs/2.4/howto/htaccess.html .


Screenshot from the official apache mod-rewrite manual:

htaccess vs server.config file apache

What is a server.config file?



It is a server level config file located in your httpd server root. You can put directives in this file to customize the behaviour of your server.

What is an htaccess file?


htaccess is a directory leve config file located in your document root.
By directory level I mean to say that you can use it in your main directory if you want the htaccess to effect the folder and its subfolders or you can also use it in a particular folder/subfolder so that it can affect the folder accordingly.

Not all hosting providers allow you to access server.config


Some paid or free webhosting providers do not allow you to edit server.config on their server that means that you only have access to a dynamic configuration file called .htaccess.
htaccess file can be placed anywhere in any directory .
Even wordpress installation contains an htaccess file to create short and friendly URLs.
But the question "Should I use htaccess or server.config file " still keeps popping up in our mind and everyone seems to be curious to know it.

server.config or htaccess?


If you do not have access to the main config file on your server then You can use htaccess. There is nothing wrong with using an htaccess file. You won't lose your seo rank. But there are some performance issues that can effect your site.
Apache mod rewrite documentation says that "htaccess file slows down the performance of a webserver" . Yes it does..but very little 0.5 or 1%. You can't even notice how much your server performance and page load speed is affected.
So you don't have be worried about it.
The difference between a server.config and an htaccess file is that you a server.config file supports all directives while an htaccess doesn't like RewriteMap and there are some other useful directives that you cant use in an htaccess file.

Another big diffrence between these contexts is how a web server reads/executives these files. A server.config file is read by server only once. A htaccess file is read on each request. When you request sa file or directory from webserver , your webserver first reads the htaccess to serve you the file dir according to commands you put in htaccess. Server.config file on the on other is read only onec you update it.

To better understand how an htaccess file can slow down your webserver, consider the following example :


Say you an htaccess file in your directory to rewrite "/file.php" to "/file2.php" .


You type "/file.php" in your browser address bar.

You webserver receive the request "/file.php" .
In order to serve you the "/file2.php" your server reads your htaccess two times.
1) Your server reads your htaccess contents and rewrites "/file.php" to "/file2.php" .
2) Your server again reads your htaccess file to serve you the destination file "/file2.php" .
As you can see your webserver read your htaccess two times to serve a simple request. Then obviously your page load speed will degrees by 1 or 2% .

Post a Comment

0 Comments