首页 > 解决方案 > 如何使用 .htaccess 规则将 .php 重写为 .html?

问题描述

我尝试过,但失败了。原始网址

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/$1.html [R=301,L]  

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

标签: apache.htaccessredirectmod-rewriteurl-rewriting

解决方案


请您尝试以下操作。使用您展示的样本进行编写和测试。请确保在测试 URL 之前清除浏览器缓存。

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

推荐阅读