我想从
转换网址http://localhost/projectname/api/index.php?type=login
到
http://localhost/projectname/api/login
答案 0 :(得分:2)
转换不是这里的术语。
在您的案例中使用htaccess的问题是,来自浏览器的请求会发生什么?。如果您愿意,浏览器中的请求看起来像http://example.com/projectname/api/login
,但在内部它应该http://example.com/projectname/api/index.php?type=login
,而这称为重写。
另一个选择是您想要重定向,这意味着如果浏览器正在请求,例如http://example.com/projectname/api/login
服务器使用正确的网址进行回复,例如http://example.com/projectname/api/index.php?type=login
和浏览器现在即时加载此页面。如果您在浏览器中对此进行测试,则会看到该网址会发生变化。
因此,对于内部重写,您可以使用:
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
重定向或重写为/projectname/api/index.php?type=login
或/projectname/api/logout
至/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]
如果您有一些HTML输出,并且想要在输出到浏览器之前更改输出,.htaccess无法帮助您,您必须在PHP应用程序中执行此操作。