.htaccess 如何在php中将查询字符串转换为斜杠URL?
我想从中转换URL.htaccess 如何在php中将查询字符串转换为斜杠URL?,.htaccess,.htaccess,我想从中转换URL http://localhost/projectname/api/index.php?type=login 到 转换在这里不是一个GET术语 在您的案例中,htaccess的问题是,来自浏览器的请求应该发生什么情况。如果您喜欢,则来自浏览器的请求如下所示http://example.com/projectname/api/login 但在内部,它应该这样做http://example.com/projectname/api/index.php?type=login 这叫做重
http://localhost/projectname/api/index.php?type=login
到
转换在这里不是一个GET术语
在您的案例中,htaccess的问题是,来自浏览器的请求应该发生什么情况。如果您喜欢,则来自浏览器的请求如下所示http://example.com/projectname/api/login 但在内部,它应该这样做http://example.com/projectname/api/index.php?type=login 这叫做重写
另一个选项是您想要重定向,这意味着如果浏览器正在请求,例如。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到/projectname/api/index.php?type=login或/projectname/api/logout到/projectname/api/index.php?type=logout
对于core,也可以通过另一种方式进行重写:
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应用程序中执行此操作。您希望在Apache配置标记或PHP脚本标题(类似于project.htaccess)中处理此问题吗
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]