简体   繁体   English

Refactor.htaccess 以满足友好的 url 目标

[英]Refactor htaccess to meet friendly url goal

I have urls that look like this ($_GET):我的网址看起来像这样($_GET):

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

Based on SPA Structure:基于SPA结构:

where search: is a controller for searches;其中 search: 是 controller 用于搜索; which can receive additional parameters.它可以接收额外的参数。

where Other: 1,2,3,4,5 are additional parameters, there is not a defined number of parameters, they can be less or more.其中 Other: 1,2,3,4,5 是附加参数,没有定义参数的数量,可以少也可以多。

I was thinking of creating a friendly url something like:我正在考虑创建一个友好的 url,例如:

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

The.htaccess i started testing looks like this:我开始测试的.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>

and I have 2 questions:我有两个问题:

  1. Is it correct, maybe, or can it be improved even more?它是否正确,也许,还是可以进一步改进?
  2. How do I achieve it?我如何实现它?

I have seen previous answers suggested by the site's search engine but they are far from what I hope to do:我已经看到该站点的搜索引擎建议的以前的答案,但它们与我希望做的相去甚远:

https://stackoverflow.com/a/45075219/20284348 https://stackoverflow.com/a/45075219/20284348
Problems configure .htaccess for friendly url问题配置 .htaccess 为友好 url
Taxonomy url change to friendly url分类 url 更改为友好 url
https://stackoverflow.com/a/55696789/20284348 https://stackoverflow.com/a/55696789/20284348

They do not seem valid to me or may even be obsolete.它们对我来说似乎无效,甚至可能已经过时。

If you don't know which parameters will be provided, you should provide the full string in a single GET parameter and parse it in your PHP code.如果您不知道将提供哪些参数,您应该在单个 GET 参数中提供完整的字符串,并在您的 PHP 代码中对其进行解析。

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

Index.php:索引.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];
    }
}

I directly populated the superglobal $_GET , but a properer way would be to set other variables which would be used instead of $_GET.我直接填充了 superglobal $_GET ,但更合适的方法是设置其他变量来代替 $_GET。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM