.htaccess for PHP & HTML (Simple Guide + Examples)

How to use .htaccess to control URLs, redirects, security and more.

Guide • .htaccess • 4XFree

By 4XFree – Daily AI & Tech Tips

1️⃣ What is .htaccess?

.htaccess is a configuration file used on Apache web servers.

Where to create?

2️⃣ Basic .htaccess Structure

Example base file:

# Enable rewrite engine (for pretty URLs)
RewriteEngine On

# Example: redirect old page to new page
Redirect 301 /old-page.html https://yourdomain.com/new-page.html

3️⃣ Redirect HTML or PHP Pages (301 Redirects)

Use this when you change URLs but don’t want to lose SEO traffic.

Redirect one HTML file to another

Redirect 301 /old-article.html https://yourdomain.com/new-article.html

Redirect a PHP page

Redirect 301 /old-page.php https://yourdomain.com/new-page.php

Redirect entire site to new domain

Redirect 301 / https://newdomain.com/

4️⃣ Force HTTPS (http → https)

Important for security and SEO.

<IfModule mod_rewrite.c>
RewriteEngine On

# If not HTTPS, redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

5️⃣ Remove .php or .html from URLs

So page.php becomes /page in browser. Works for both PHP and HTML.

Remove .php extension

<IfModule mod_rewrite.c>
RewriteEngine On

# Remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
</IfModule>

Now:

Remove .html extension

<IfModule mod_rewrite.c>
RewriteEngine On

# Remove .html extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
</IfModule>

Now:

6️⃣ Single front controller (index.php) – good for PHP apps

This is used in many PHP frameworks. All requests go through index.php.

<IfModule mod_rewrite.c>
RewriteEngine On

# If file or folder exists, don't touch
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Otherwise send everything to index.php
RewriteRule ^(.*)$ index.php [L]
</IfModule>

Good for:

7️⃣ Process HTML files as PHP (run PHP inside .html)

If you want to write PHP code in an .html file, some hosts allow this:

# Let .html files be handled by PHP
AddType application/x-httpd-php .php .html

Warning: This depends on your hosting configuration. On some modern servers, method can be different (e.g. PHP-FPM). If it doesn’t work, ask your host: “How to run PHP in .html files?”

8️⃣ Custom Error Pages (404, 403, 500)

You can show your own HTML/PHP page when error happens.

# Custom error pages
ErrorDocument 404 /404.html
ErrorDocument 403 /403.html
ErrorDocument 500 /500.html

Then create files: 404.html, 403.html, 500.html in your site root.

9️⃣ Basic Security Rules (.htaccess)

Block directory listing (hide files list)

# Disable directory listing
Options -Indexes

Protect .htaccess itself

<Files ".htaccess">
  Require all denied
</Files>

Block access to config .php files in a folder

<FilesMatch "config\.php$">
  Require all denied
</FilesMatch>

🔟 Cache static files (speed up your site)

Helps performance for CSS, JS, images, HTML.

<IfModule mod_expires.c>
ExpiresActive On

# Images
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png  "access plus 1 month"
ExpiresByType image/gif  "access plus 1 month"
ExpiresByType image/webp "access plus 1 month"

# CSS & JS
ExpiresByType text/css           "access plus 7 days"
ExpiresByType application/javascript "access plus 7 days"

# HTML
ExpiresByType text/html "access plus 1 hour"
</IfModule>

✅ Summary – .htaccess for PHP & HTML

Always test carefully after editing .htaccess. A small typo can break the site. If that happens, simply rename or remove the file to restore the site.

⬅ Back to Home (4XFree)