htaccess 将多查询字符串重写为路径
htaccess rewrite a multi-query string into a path
我将如何更改查询字符串
file.php?id=number&string=some-words
进入这个
file/number/some-words/
我知道这个问题之前已经被问过一百万次了,但我在这里查看了很多解决方案,它们都是基于单一查询的(比如?something 而不是?something&something-else)。
同样,一旦重写,php 在使用 $_GET 或 $_REQUEST 等时是否仍然读取原始页面查询字符串......即使它现在显示为路径?
感谢任何帮助。
谢谢。
您可以将代码放在 Apache .htaccess 文件中。这可能看起来像这样:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^users/(\d+)*$ ./profile.php?id=
RewriteRule ^threads/(\d+)*$ ./thread.php?id=
RewriteRule ^search/(.*)$ ./search.php?query=
或者你可以只使用 htaccess 和 php:
htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
PHP
<?php
#remove the directory path we don't want
$request = str_replace("/envato/pretty/php/", "", $_SERVER['REQUEST_URI']);
#split the path by '/'
$params = split("/", $request);
?>
它仍然会读取原始页面查询字符串。
RewriteRule
接受一个正则表达式,它可以像你想要的那样复杂,后面跟着真正的 URL 将加载你的文件。您在正则表达式中要捕获的部分加上括号,并且可以在第一组中引用 </code>,在第二组中引用 <code>
,在 URL 部分中以此类推。例如:
RewriteRule ^(\w+)/(\d+)/(.*)$ index.php?file=&id=&words=
这将匹配 3 个组:
- letters/numbers 到第一个斜线
- 一些数字直到第二个斜杠
- 之后的任何内容,包括额外的斜杠
并且可以通过 </code>、<code>
、</code> 引用,如第二部分 index.php.</p> 所示
<p>唯一的问题是,如果您缺少任何一部分,规则中的模式将不匹配。因此,您要么需要为每个变体制定单独的规则:</p>
<pre><code>#matches all 3 parts
RewriteRule ^(\w+)/(\d+)/(.*)$ index.php?file=&id=&words=
#matches the first 2 parts
RewriteRule ^(\w+)/(\d+)$ index.php?file=&id=
#matches just the first part
RewriteRule ^(\w+)$ index.php?file=
#matches everything else
RewriteRule ^.*$ index.php
或者您可以执行通常称为 bootstrapping
的操作,即使用单个 RewriteRule
将所有内容重定向到单个 php 文件,如下所示:
RewriteRule ^(.*)$ index.php
然后您可以使用 php 来确定不同的部分是什么。在 php 里面有一个内置的服务器变量 $_SERVER['REQUEST_URI']
会给你 url 的 URI
部分,它是域和第一个斜杠之后的所有内容,包括任何查询字符串参数.这是基于用户请求的 URL 而不是 apache 重写的那个。您可以 explode('/', $_SERVER['REQUEST_URI'])
获取各个部分并用它们做任何您想做的事情。
我将如何更改查询字符串
file.php?id=number&string=some-words
进入这个
file/number/some-words/
我知道这个问题之前已经被问过一百万次了,但我在这里查看了很多解决方案,它们都是基于单一查询的(比如?something 而不是?something&something-else)。
同样,一旦重写,php 在使用 $_GET 或 $_REQUEST 等时是否仍然读取原始页面查询字符串......即使它现在显示为路径?
感谢任何帮助。
谢谢。
您可以将代码放在 Apache .htaccess 文件中。这可能看起来像这样:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^users/(\d+)*$ ./profile.php?id=
RewriteRule ^threads/(\d+)*$ ./thread.php?id=
RewriteRule ^search/(.*)$ ./search.php?query=
或者你可以只使用 htaccess 和 php:
htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^.*$ ./index.php
PHP
<?php
#remove the directory path we don't want
$request = str_replace("/envato/pretty/php/", "", $_SERVER['REQUEST_URI']);
#split the path by '/'
$params = split("/", $request);
?>
它仍然会读取原始页面查询字符串。
RewriteRule
接受一个正则表达式,它可以像你想要的那样复杂,后面跟着真正的 URL 将加载你的文件。您在正则表达式中要捕获的部分加上括号,并且可以在第一组中引用 </code>,在第二组中引用 <code>
,在 URL 部分中以此类推。例如:
RewriteRule ^(\w+)/(\d+)/(.*)$ index.php?file=&id=&words=
这将匹配 3 个组:
- letters/numbers 到第一个斜线
- 一些数字直到第二个斜杠
- 之后的任何内容,包括额外的斜杠
并且可以通过 </code>、<code>
、</code> 引用,如第二部分 index.php.</p> 所示
<p>唯一的问题是,如果您缺少任何一部分,规则中的模式将不匹配。因此,您要么需要为每个变体制定单独的规则:</p>
<pre><code>#matches all 3 parts
RewriteRule ^(\w+)/(\d+)/(.*)$ index.php?file=&id=&words=
#matches the first 2 parts
RewriteRule ^(\w+)/(\d+)$ index.php?file=&id=
#matches just the first part
RewriteRule ^(\w+)$ index.php?file=
#matches everything else
RewriteRule ^.*$ index.php
或者您可以执行通常称为 bootstrapping
的操作,即使用单个 RewriteRule
将所有内容重定向到单个 php 文件,如下所示:
RewriteRule ^(.*)$ index.php
然后您可以使用 php 来确定不同的部分是什么。在 php 里面有一个内置的服务器变量 $_SERVER['REQUEST_URI']
会给你 url 的 URI
部分,它是域和第一个斜杠之后的所有内容,包括任何查询字符串参数.这是基于用户请求的 URL 而不是 apache 重写的那个。您可以 explode('/', $_SERVER['REQUEST_URI'])
获取各个部分并用它们做任何您想做的事情。