简体   繁体   English

根据URI段进行重定向

[英]Redirect depending on URI Segment

I am using Codeigniter and want to have SEO-friendly URLs. 我正在使用Codeigniter,并希望拥有SEO友好的URL。 There will be 2 types of URI segments, http://www.domain.com/view/193847 and http://www.domain.com/view/193847-canon-5d-mark-iii . 将会有2种URI分段,即http://www.domain.com/view/193847http://www.domain.com/view/193847-canon-5d-mark-iii

If the first URL is used, function view_mini is called and passed the product id 193847 . 如果使用第一个URL,则调用view_mini函数并传递产品ID 193847 If the 2nd one is used, function view_full will be called and passed the same product id 193847 . 如果使用第二个,则将调用函数view_full并传递相同的产品ID 193847

Problem: How can I differentiate between the 2 URLs? 问题:如何区分两个URL? Or is this an inferior approach to solve the problem? 还是这是解决问题的劣等方法?

PHP How should the if condition be structured? PHP if条件应该如何构造?

function view($pid) {
    if($this->uri->segment(2) == something) {
        $this->view_mini($pid);
    } else {
        $this->view_full($pid);
        }
}

function view_mini($pid) {
   // ...
}

function view_full($pid) {
   // ...
}

EDIT 编辑

I am using URL routing to route http://www.domain.com/controllername/view/1234 to http://www.domain.com/view/1234 我正在使用URL路由将http://www.domain.com/controllername/view/1234路由到http://www.domain.com/view/1234

Edit: un-tested 编辑:未经测试

$route["view/(\d+)"] = "class/view_mini/$1";
$route["view/(\d+)-([\w'-]*)?/g"] = "class/view_full/$1/$2";

Is there any definitive structure to the different URLs? 不同的URL有明确的结构吗?

ie. 即。

http://www.domain.com/view/[numbers]-[text]

If so, you could test the URL for that dash between the numbers and the dash? 如果是这样,您可以测试数字和破折号之间的破折号的URL吗?

you can use the regular expression to check the segment if it has anything other than numbers, then you can execute the view that you want for example 您可以使用正则表达式来检查段中是否包含数字以外的任何内容,然后可以执行所需的视图,例如

$pattern = '\d[0-9][^a-zA-Z]';
$url = $this->uri->segment(2);
if(preg_match($pattern,$url))
{
//this will match only the numbers

$this->view_mini($pid);
    } else {
        $this->view_full($pid);
    }

i hope this will help .. 我希望这个能帮上忙 ..

Use 采用

$result = explode('-', $this->uri->segment(1));

If(isset($result[1]))
{
    // show full view
} else {

    // show mini view

}

$pid will always be $result[0] $ pid将始终是$ result [0]

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

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