简体   繁体   English

用多个参数重写URL PHP Apache

[英]Rewrite url with multiple params PHP Apache

I just spent a few hours looking around here but didn't really find a solution. 我只是花了几个小时在这里四处逛逛,但并没有真正找到解决方案。

I currently have typical query string like; 我目前有典型的查询字符串,例如;

 http://www.site/jobs/results.php?keyword=Accounting+%26+Finance&type=10&state_id=130&location=NSW&order=1&page=1

which Id like to rewrite to 我想重写为

 http://www.site/jobs/find/accounting-finance/NSW/free-jobs/1/?order=1

but I don't know what params will sent, that will depend on what the user does, filters,categories,orders etc 但我不知道会发送什么参数,这取决于用户的操作,过滤器,类别,订单等

I would like to keep it simple and parse the url in PHP and use a simple rewrite rule, but then I need key/value pairs so I know which value belongs to which param something like; 我想保持它的简单,并使用PHP解析URL并使用简单的重写规则,但是随后我需要键/值对,以便知道哪个值属于哪个参数;

http://www.site/jobs/find/keyword/accounting-finance/state/NSW/type/free-jobs/page/1/?order=1

I'm told that is not a good option for seo. 有人告诉我,这不是seo的好选择。

Apart from writing many different rules in .htacces to cover all the scenarios can you suggest a better way to approach this 除了在.htacces中编写许多不同的规则以涵盖所有情况之外,您还可以建议一种更好的方法来解决此问题

Thanks 谢谢

.htaccess : .htaccess:

RewriteEngine on
# skip rewriting if file/dir exists (optionally)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite all to results.php
RewriteRule . results.php

php (simple way with key=>value pairs): php(使用key => value对的简单方法):

// current URI (/jobs/find/keyword/accounting-finance/state/NSW/type/free-jobs/page/1/?order=1)
$path = $_SERVER['REQUEST_URI'];

// remove base path (/jobs)
if (($len = strlen(basename($_SERVER['SCRIPT_FILENAME'])))) 
    $path = substr($len, $path);

// remove GET params (?order=1)
if (false !== ($pos = strpos($path, '?'))) 
    $path = substr($path, 0, $pos);

$path = explode('/', trim($path, '/'));

// extract action (or whatever 'find' is)
$action = array_shift($path);

// make key => value pairs from the rest
$params = array();
for ($i = 1, $c = count($path) ; $i < $c ; $i += 2) {
    $params[urldecode($path[$i - 1])] = urldecode($params[$i]);
    // or put it to GET (only remember that it will overwrite already existing values)
    //$_GET[urldecode($path[$i - 1])] = urldecode($params[$i]);
}

you can modify this script to achieve values only without keys, but here comes the question - is it possible to determine if value should be one key or another? 您可以修改此脚本以仅在没有键的情况下实现值,但这是一个问题-是否可以确定值应该是一个键还是另一个键? If params are always on same position and you can only get less or more of them, then its pretty easy: 如果参数始终处于同一位置,而您只能获得更少或更多的参数,那么这很简单:

// skip this step from previous example
//$action = array_shift($path);    

$params = array(
    'action' => null,
    'keyword' => null,
    'state' => null,
    'type' => null,
    'page' => null,
);
$keys = array_keys($params);
for ($i = 0 , $c = min(count($path), count($keys) ; $i < $c ; ++$i) {
    $params[$keys[$i]] = urldecode($path[$i]);
}

But if you don't know which param is on which position then things are going to be more complex. 但是,如果您不知道哪个参数位于哪个位置,那么事情将会变得更加复杂。 You would need to do some checks on every param and determine which one it is - if all of those values are selected from some known lists of values then it also won't be very difficult, for example: 您将需要对每个参数进行一些检查并确定它是哪一个-如果所有这些值都是从某些已知值列表中选择的,那么也就不会很困难,例如:

$params = array(
    'action' => null,
    'keyword' => null,
    'state' => null,
    'type' => null,
    'page' => null,
);
$params['action'] = array_shift($path);
$keys = array_keys($params);
foreach ($path as $value) {
    if (is_numeric($value)) $params['page'] = intVal($value);
    else {
        $key = null;
        // that switch is not very nice - because of hardcode
        // but is much faster than using 'in_array' or something similar
        // anyway it can be done in many many ways
        switch ($value) {
            case 'accounting-finance' :
            case 'keyword2' :
            case 'keyword3' :
                $key = 'keyword';
                break;
            case 'NSW' :
            case 'state2' :
                $key = 'state';
                break;
            case 'type1' :
            case 'type2' :
            case 'type3' :
                $key = 'type';
                break;
            // and so on...
        }
        if ($key === null) throw new Exception('Unknown value!');
        $params[$key] = $value;
    }
}

You can also to try write some really complex regexes in .htaccess, but IMO it is not a place for that - apache should match request with correct endpoint in your application and run it, its not place for extended params logic (if anyway it will go to the same place in your app). 您也可以尝试在.htaccess中编写一些非常复杂的正则表达式,但是IMO并不是这样做的地方-apache应该将请求与应用程序中的正确端点匹配并运行它,而不是扩展参数逻辑的地方(如果无论如何它将转到应用中的同一位置)。 Also its much more convenient to keep that logic in app - when you're changing something you can do that in app code without need of changing anything in htaccess or apache config (in productional environment I'm mostly moving .htaccess contents to apache config and turning off .htaccess support - that gives some speedup when apache is not searching for those files, but any change require apache restart). 将逻辑保留在应用程序中也更加方便-更改内容时,可以在应用程序代码中执行此操作,而无需更改htaccess或apache config中的任何内容(在生产环境中,我主要将.htaccess内容移至apache config中并关闭.htaccess支持-当apache不搜索那些文件,但是任何更改都需要重新启动apache时,可以加快速度。

If all you want to do is push anything SEO-relevant into the URL, your rewriting actually gets very easy: Just don't do any rewriting at all, keep the parameters where they are, just fill up the spot after results.php with your SEO keywords: 如果您要做的只是将任何与SEO相关的内容推入URL,那么您的重写实际上就变得非常容易:根本不做任何重写,将参数保留在原来的位置,只需在results.php之后填入以下内容:您的SEO关键字:

http://myurl/jobs/results.php/seo-keywords/gohere/as-you-like-it?keyword=Accounting+%26+Finance&type=10&state_id=130&location=NSW&order=1&page=1

It should not make a difference to PHP because this nonexistant path ends up getting parsed into $_SERVER['PATH_INFO'], but the script execution stays the same. 它不会对PHP造成影响,因为该不存在的路径最终将被解析为$ _SERVER ['PATH_INFO'],但是脚本执行保持不变。

Problem is now: What is the source of your SEO relevant keywords? 现在的问题是:您的SEO相关关键字的来源是什么? :) :)

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

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