如何使用 .htaccess 规则将 .php 重写为 .html?

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

我尝试过但失败了。 原文URL

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

URL 浏览器应该显示

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

这是我试过的方法,但没有用

RewriteEngine on  
RewriteBase /

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

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

能否请您尝试以下。使用您显示的示例编写和测试。请确保在测试您的 URL 之前清除浏览器缓存。

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