重写然后使用 .htaccess 重定向到同一页面

rewrite then redirecting to same page with .htaccess

默认URL:domain.com/contact.php

将 URL 重写为:domain.com/contactus

然后当我必须输入默认 URL 到:domain.com/contact.php 它应该重定向(打开)到 domain.com/contactus

我试过了

RewriteRule ^contactus$ contact.php [L]
RewriteRule contact.php contactus [NC,R=301,L]

您需要一个重定向规则来在浏览器中强制使用新的 URI,并需要一个重写规则来将新的 URI 转发到 php 文件:

RewriteEngine On

# redirect rule
RewriteCond %{THE_REQUEST} \s/contact\.php[?\s] [NC]
RewriteRule ^ /contactus? [L,R=301] 

# rewrite rule
RewriteRule ^contactus/?$ contact.php [L,NC]
目标 URI 中的

? 将删除现有的查询字符串。