重构htaccess以满足友好的url目标



我有如下url:

http://localhost/index.php?search=param& other1 = param1& other2 = param2&中= param3& other4 = param4

基于SPA结构:

其中search:是用于搜索的控制器;可接收附加参数

其中Other: 1、2、3、4、5为附加参数,参数个数没有规定,可少可多

我正在考虑创建一个友好的url,如:

http://localhost/search=param/other1=param1/other2=param2/other3=param3/other4=param4

我开始测试的htaccess是这样的:

php_value display_errors On
php_value mbstring.http_input auto
<IfModule mod_rewrite>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

和我有两个问题:

  1. 这是正确的,也许,还是可以进一步改进?
  2. 如何实现?

我已经看到了以前网站搜索引擎给出的答案,但它们与我希望做的相差甚远:

https://stackoverflow.com/a/45075219/20284348
问题配置。htaccess为友好url
分类法url更改为友好url
https://stackoverflow.com/a/55696789/20284348

它们在我看来是无效的,甚至可能已经过时了。

如果您不知道将提供哪些参数,您应该在单个GET参数中提供完整的字符串,并在PHP代码中解析它。

RewriteRule ^(.*)$ index.php?params=$1 [L]

index . php:

<?php
if ( isset( $_GET['params'] ) ) {
// splits the slash-separated params string into an array
$strParams = explode('/', $_GET['params']);
foreach ( $strParams as $strParam) {
$matches = [];
// look for `=` char to separate parameter name and value
preg_match('/([^=]*)=(.*)/', $strParam, $matches);
// Populate $_GET using parameter name as key
$_GET[$matches[1]] = $matches[2];
}
}

我直接填充超全局$_GET,但正确的方法是设置其他变量,而不是使用$_GET。

最新更新