Technology

Redirecting All Files to a Single File in .htaccess Using Rewrite Rules

Learn how to redirect all files to a single file in .htaccess using Rewrite rules, ensuring proper handling of static files and avoiding infinite looping with precise conditional checks.

Sangrak
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [NC,L]

By leveraging the options in RewriteCond, when a URL points directly to an existing filename or directory, those resources are loaded directly. The ! signifies negation, -d checks for the existence of a directory, and -f checks for an existing file.

These options are crucial not just for loading static files efficiently but most importantly, to prevent infinite rewriting loops that could occur when the requested URL matches the filename index.php. This setup ensures your URLs are SEO-friendly and your server configuration is optimized for both static and dynamic content handling.

Reference: http://apmusers.com/tt/dbckdghk/95

More on this topic