强烈建议您阅读此“Apache mod_rewrite 简介”
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*)\.php [NC]
RewriteRule ^ /%1 [R=301,QSA,L]
# Redirect to hello
RewriteRule ^marketplace$ /hello [R=301,NC,L]
# Show the content of marketplace on hello
RewriteRule ^hello$ /marketplace.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
以上是经过充分测试和工作的规则。
第一条规则将重定向.php到 URL 之类的目录,例如domain.com/marketplace.php变为domain.com/marketplace.
第二条规则重定向marketplace到hello.
第三条规则在内部重定向hello到,marketplace.php因此 URL 保留hello为marketplace.php.
最后一条规则将验证它的目录不存在但作为 php 文件存在并在内部重定向到它。
因此,如果您访问marketplace.php它,marketplace然后hello从 hello 访问它,您将看到marketplace.php.
如果marketplace.php不存在,则会出现 404 错误。