How to rewrite .php to .html using .htaccess rules?

Viewed 53

I tried and failed. Original URL

https://www.sitename.com/user/login/index.php
https://www.sitename.com/user/dashboard/index.php

URL that the browser should show

https://www.sitename.com/user/login.html
https://www.sitename.com/user/dashboard.html

This is what I tried and it doesnt work

RewriteEngine on  
RewriteBase /

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

RewriteCond %{THE_REQUEST} (.*)\.html  
RewriteRule ^(.*)\.html http://%{HTTP_HOST}/user/$1.php [L]
1 Answers

Could you please try following. Written and tested with your shown samples. Please make sure you clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^.]*)\.html/?$ [NC]
RewriteRule ^(.*)$ %1/%2/index.php [L]
Related