This .htaccess rule ensures that incoming requests are redirected through index.php on Apache servers if the mod_rewrite module is enabled. This structure is especially used in SEO-friendly (pretty URL) systems and PHP applications with an MVC structure.
Code Explanation:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
Detailed Meaning:
-
: These rules work ifmod_rewriteis installed. If it is not installed, it is ignored. -
RewriteEngine On: Activates the URL rewriting engine. -
RewriteCond %{REQUEST_FILENAME} !-d: Continues if the requested address is not a physical folder. -
RewriteCond %{REQUEST_FILENAME} !-f: Continues if the requested address is not a physical file. -
RewriteRule ^(.*)$ index.php [QSA,L]: Redirects all other requests to theindex.phpfile.
Meaning of [QSA,L]:
-
QSA(Query String Append): Preserves the query parameters of the original URL. -
L(Last): No other redirection rule will run after this rule.
Example:
http://siteadi.com/hakkimizda
If this request is not actually a physical file or folder, it is redirected to index.php. Redirection can be done on the PHP side with $_SERVER['REQUEST_URI'] or $_GET.
Usage Areas:
-
In PHP MVC Frameworks (Laravel, CodeIgniter, Slim)
-
In CMS infrastructures such as WordPress, Joomla
-
When setting up an SEO-friendly URL system
Requirements:
-
The
mod_rewritemodule must be active on the Apache server. -
The use of
.htaccessfiles must be allowed withAllowOverride Allpermission.
This structure is commonly used in dynamic web applications to simplify URL management and control redirects centrally through index.php.