Codeigniter 第一段 URL 变量
我希望使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
补充一下马修的回应。
这就是您在
system/application/config/routes.php
文件中需要的内容:这将重定向所有内容。
如果您需要使用其他控制器,您可能不想重定向所有内容。如果是这种情况,您可以使用此正则表达式:
这将允许您重定向除控制器“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: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:
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.我自己还没有玩过路由,但我认为你可以尝试像这样的事情:
不过我自己还没有尝试过。
I myself haven't played a whole lot with Routing, but I think you could try something like this:
Haven't tried this myself though.
好吧,尽管将某些区域列入白名单似乎有点不标准,而且我仍在思考如何处理 404 错误,但这里有一个目前正在使用的方法,使用来自进化的正则表达式。我的 home.php 控制器文件:
class Home extends Controller {
}
/* 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 {
}
/* EOF */
Thanks for the help, please post alternate soln's if available.