vin
vin

Reputation: 562

Replacing .php ext with .html through .htaccess for root files only not subdirectoy files

How we can replace .php ext with .html through .htaccess for root files only not subdirectoy files.

Example : www.test.com/home.php should be change to www.test.com/home.html but www.test.com/admin/home.php should remain same.

I simply tried as below:

RewriteEngine on  
RewriteBase /

RewriteCond %{THE_REQUEST} (.*)\.php  
RewriteRule ^(.*)\.php $1.html [R=301,L]  

RewriteCond %{THE_REQUEST} (.*)\.html  
RewriteRule ^(.*)\.html $1.php [L] 

Please assist me.

Upvotes: 1

Views: 166

Answers (2)

kingprawn
kingprawn

Reputation: 336

Change your .htaccess file to:

RewriteEngine on  
RewriteBase /

RewriteRule ^([^/]*)\.php$ $1.html [R=301,L]
RewriteRule ^([^/]*)\.html$ $1.php [L]

Then update all links to your root pages to link to *.html

Upvotes: 0

anubhava
anubhava

Reputation: 785276

You can use code like this in root .htaccess:

RewriteEngine on  
RewriteBase /

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

RewriteRule ^([^/.]+)\.html $1.php [L,NC]

[^/.]+ regex will make sure to make it work in root path only.

Upvotes: 2

Related Questions