What is .htaccess file? And in .htaccess how to rewrite url folder/file.php to folder/file?


An .htaccess file is a directory-level configuration file supported by several web servers. It is used for configuration of website-access issues, such as URL redirection, URL shortening, access control, customized error responses, cache control and more.
When a .htaccess file is placed in a directory which is in turn 'loaded via the Apache Web Server', then the .htaccess file is detected and executed by the Apache Web Server software.
Following code need to be written in .htaccess file to rewrite url folder/file.php to folder/file:

RewriteEngine On
RewriteRule ^folder/([^/]*)$ folder/file.php [L]

Below is another variation where we can pass parameter in the url:

RewriteRule ^folder/file/([^/]*)$ folder/file.php?name=$1  [L]

The output of the above url will be something like this if the parameter we are passing in name as file-1:


Related #tags:     #php     #.htaccess     #apache    

Related questions

Looking for change? Click below to prepare for technical interview.

Technical Interview Questions