.htaccess Rewrite Rules for Website

In stock

Category:

This .htaccess code is used for URL rewriting in Apache web servers, primarily for websites.
Let’s break it down line by line:

.htaccess Rewrite Rules for Website


<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

 

________________________________________
🛠️ <IfModule mod_rewrite.c>
Checks if the mod_rewrite module is enabled in Apache.
If mod_rewrite is available, Apache will execute the rules inside this block.

________________________________________
🚀 RewriteEngine On
This activates the URL rewriting engine.
It allows dynamic URL modifications.

________________________________________
🔑 RewriteRule . – [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
• [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] → Ensures the HTTP_AUTHORIZATION header is passed through.
• This rule preserves HTTP authentication headers for scripts, APIs, or plugins that require authentication.
🟢 . → Matches all requests.*
🟢 – → Means no substitution (doesn’t rewrite the URL).

________________________________________
📂 RewriteBase /
Defines the base directory for rewrite rules.
/ means it’s applied to the root of the site.

________________________________________
📜 RewriteRule ^index.php$ – [L]
If the requested URL is exactly index.php, do nothing.
🟢 – → No change.
🟢 [L] → Last rule, stopping further processing if matched.

________________________________________
📁 RewriteCond %{REQUEST_FILENAME} !-f
This checks if the requested file does NOT exist (!-f).
If the request is for an actual file (like image.jpg), it skips the next rule.

________________________________________
📂 RewriteCond %{REQUEST_FILENAME} !-d
This checks if the requested directory does NOT exist (!-d).
If the request is for a real directory, it skips the next rule.

________________________________________
🔄 RewriteRule . /index.php [L]
If no actual file or directory exists, redirects all requests to index.php.
This is how WordPress handles pretty URLs (Permalinks).
🟢 [L] → Stops further rule processing.
________________________________________

🔹 Summary
If a user requests a real file or folder, it is served directly.
If the requested URL does not match an existing file or directory, it is redirected to index.php, allowing the website to handle dynamic URLs.

Main Menu

Rewrite Rules for Website

.htaccess Rewrite Rules for Website