Convert在这里不是一个常用术语。
在您的情况下, htaccess 的问题是,即将到来的请求会发生什么从浏览器。如果你喜欢来自浏览器的请求看起来像http://example.com/projectname/api/login
但在内部它应该做http://example.com/projectname/api/index.php?type=login
这就是所谓的rewrite.
另一种选择是您想要一个redirect,这意味着如果浏览器正在请求,例如http://example.com/projectname/api/login
服务器以正确的 URL 响应,例如http://example.com/projectname/api/index.php?type=login
浏览器现在立即加载此页面。如果您在浏览器中对此进行测试,您将看到 URL 将发生变化。
因此,对于内部重写,您可以使用:
RewriteEngine On
# Rewrite from e.g. /projectname/api/login to /projectname/api/index.php?type=login
RewriteRule ^/?projectname/api/(.*)$ /projectname/api/index.php?type=$1 [QSA,L]
对于您所做的重定向
RewriteEngine On
# Redirect from e.g. /projectname/api/login to /projectname/api/index.php?type=login
RewriteRule ^/?projectname/api/(.*)$ /projectname/api/index.php?type=$1 [R=301,QSA,L]
这将重定向或重写,例如/projectname/api/login
to /projectname/api/index.php?type=login
or /projectname/api/logout
to /projectname/api/index.php?type=logout
核心这也是可能的反过来用于重写:
RewriteEngine On
# Rewrite from e.g. /projectname/api/index.php?type=login to /projectname/api/login
RewriteCond %{QUERY_STRING} ^type=([^&]*)$
RewriteRule ^/?projectname/api/index.php$ /projectname/api/%1 [L]
也用于重定向
RewriteEngine On
# Redirect from e.g. /projectname/api/index.php?type=login to /projectname/api/login
RewriteCond %{QUERY_STRING} ^type=([^&]*)$
RewriteRule ^/?projectname/api/index.php$ /projectname/api/%1 [R=301,L]
But
如果您有一些 HTML 输出并且您想在提供输出之前更改输出到浏览器, .htaccess 无法帮助您,您必须在 PHP 应用程序中执行此操作。