Max
Max

Reputation: 1195

redirect all .html extensions to .php

I want to update all the pages on a website to use include for the footer and header. So I have to change a lot of .html pages to .php.

So i'm looking for a way to redirect all pages that end with .html to the same url but ending in .php.

Upvotes: 24

Views: 53965

Answers (7)

OliverP
OliverP

Reputation: 13

The rule need needs an / in front of $1 otherwise, at least in my cPanel, the file I'm redirected to has the full path, including the home folder, etc.

RewriteEngine On
RewriteRule ^(.*).html$ /$1.php [L,R]

Upvotes: 0

Dušan Maďar
Dušan Maďar

Reputation: 9889

If you want an actual HTTP 301 Moved Permanently Redirect

RewriteEngine on
RedirectMatch 301 ^(.*)\.html$ $1.php

or

RewriteEngine on
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1.html [L,R=301]

Upvotes: 2

Mark Waldin
Mark Waldin

Reputation: 1

Note that the AddType command will process your existing html file as php. If what you wanted was to replace an existing html file with a new php file you need to use the rewrite rule.

Upvotes: 0

wewals
wewals

Reputation: 1447

In your apache httpd.conf file you can add

AddType application/x-httpd-php .html

to make .html files go through the php parser before they are served to the user. You can also add this directive to your .htaccess file. The second method may not work depending on how your host is setup.

Upvotes: 1

Pål Brattberg
Pål Brattberg

Reputation: 4698

mod_rewrite to the rescue!

RewriteEngine On
RewriteRule ^(.+)\.html$ $1.php

Upvotes: 6

Jordonias
Jordonias

Reputation: 5848

You could do a more simple approach and have all your html files be processed as php files by adding the following line to your .htaccess

AddHandler application/x-httpd-php .php .html

Upvotes: 8

James C
James C

Reputation: 14149

RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]

If you want it to be done as a redirect instead of just a rewrite modify the [L] to [L,R]

Upvotes: 51

Related Questions