Codeigniter 第一段 URL 变量

发布于 2024-09-13 08:09:07 字数 195 浏览 3 评论 0原文

我希望使用 URL 缩短方案,其中我希望变量位于 URL 的第一段 www.example.com/0jf08h204 中。我的默认控制器是“home.php”,并且我有 .htaccess mod-rewrites,那么管理它的最佳方法是什么?我想我的智慧已经被标准 /controller/method/variable 方案阻止了,这是一个 URI 协议设置吗?谢谢你!

I am looking to use a URL shortening scheme where I would like the variable to be in the first segment of the URL, www.example.com/0jf08h204. My default controller is "home.php" and I have .htaccess mod-rewrites in place, so what is the best way to manage this? I suppose my smarts have been blocked by the standard /controller/method/variable scheme, is this a URI Protocol setting? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋日私语 2024-09-20 08:09:07

补充一下马修的回应。

这就是您在 system/application/config/routes.php 文件中需要的内容:

$route['(:any)'] = "home";

这将重定向所有内容。

如果您需要使用其他控制器,您可能不想重定向所有内容。如果是这种情况,您可以使用此正则表达式:

$route['^(?!about|contact)\S*'] = "home";

这将允许您重定向除控制器“about”或“contact”之外的所有内容 - 这些将被定向到“about.php”和“contact.php”控制器。

请注意,我选择不在 CodeIgniter 中使用通配符,您可能会发现它们更适合您,但是我选择在重定向后手动解析 $_SERVER['REQUEST_URI'] 。但是,如果您想使用通配符,只需将 /$1 添加到路由中,如 Matthew 的响应中所示。

To add to Matthew's response.

This is what you'll need in your system/application/config/routes.php file:

$route['(:any)'] = "home";

This will redirect EVERYTHING.

You might not want to redirect everything if you have other controllers which you need to use. If that is the case you can use this regular expression instead:

$route['^(?!about|contact)\S*'] = "home";

This will allow you to redirect everything except the controllers 'about' or 'contact' -- these will be directed to the 'about.php' and 'contact.php' controllers.

Please note, I choose not to use the wild cards within CodeIgniter, you might find they work better for you, I however choose to parse out the $_SERVER['REQUEST_URI'] manually after the redirect. If however you wanted to use the wildcards you would just add /$1 to the routes as you see in Matthew's response.

彼岸花似海 2024-09-20 08:09:07

我自己还没有玩过路由,但我认为你可以尝试像这样的事情:

$route['(:any)'] = "controllername/actionname/$1";

不过我自己还没有尝试过。

I myself haven't played a whole lot with Routing, but I think you could try something like this:

$route['(:any)'] = "controllername/actionname/$1";

Haven't tried this myself though.

琉璃繁缕 2024-09-20 08:09:07

好吧,尽管将某些区域列入白名单似乎有点不标准,而且我仍在思考如何处理 404 错误,但这里有一个目前正在使用的方法,使用来自进化的正则表达式。我的 home.php 控制器文件:

class Home extends Controller {

function Home()
{
    parent::Controller();
    $uri = uri_string();
    if(!empty($uri))    {
        $uri_array = explode('/',$uri);
        $first_segment = $uri_array[1];
        if(isset($first_segment))   {
            //do stuff, load alternative view
        }
    }
}

function index()
{
    $this->load->view('home_view');
}

}

/* EOF */

感谢您的帮助,请发布备用解决方案(如果有)。

Well even though it seems a bit non-standard to whitelist certain areas, and I am still running through my head how 404 errors will be handled, here is a method that is working for now using the regex from evolve. My home.php controller file:

class Home extends Controller {

function Home()
{
    parent::Controller();
    $uri = uri_string();
    if(!empty($uri))    {
        $uri_array = explode('/',$uri);
        $first_segment = $uri_array[1];
        if(isset($first_segment))   {
            //do stuff, load alternative view
        }
    }
}

function index()
{
    $this->load->view('home_view');
}

}

/* EOF */

Thanks for the help, please post alternate soln's if available.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文