使用htaccess在Codeigniter中的自定义URL [英] Custom URL in Codeigniter using htaccess

查看:113
本文介绍了使用htaccess在Codeigniter中的自定义URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Codeigniter 2.0.2上开发一个站点。它是一个网站,公司/用户可以在该网站上注册并创建自己的个人资料页面,并拥有自己的自定义网址(例如 http:// facebook。 com / demouser ),拥有自己的反馈系统,显示其服务。

I am developing a site on Codeigniter 2.0.2 . Its a site where companies/users can signup and create their own profile page, have their own custom url(like http://facebook.com/demouser), have their own feedback system, display their services.

这表示,我已成功以以下格式显示了个人资料页面

This said, I have been successful in display the profile page in the following format

http://mainwebsite.com/company/profile/samplecompany
这将显示主页对于公司 samplecompany ,其中company是控制器,配置文件是方法。

http://mainwebsite.com/company/profile/samplecompany This displays the home page for the company samplecompany , where company is the controller and profile is the method.

现在我有几个问题,


  1. 我想可以创建具有/获取 http://mainwebsite.com/samplecompany 使用htaccess和默认控制器。如果有人可以帮助htaccess规则,那就太好了。我已经在使用htacess从CI中删除index.php,但是无法正常工作。

  1. I guess it is possible to create to have/get http://mainwebsite.com/samplecompany using htaccess and a default controller. If anybody can help with the htaccess rule , that would be awesome. I am already using htacess to remove index.php from CI but could not get this working.

给定用户/公司的其他页面很少,例如作为反馈,请与我们联系,服务等。因此,我想到的实现链接的格式为
`
http://mainwebsite.com/company/profile/samplecompany/feedback
http://mainwebsite.com/samplecompany/feedback

There will be few other pages for the given user/company such as feedback, contact us, services etc. So the implementation links that come to my mind is of the form ` http://mainwebsite.com/company/profile/samplecompany/feedback or http://mainwebsite.com/samplecompany/feedback

http://mainwebsite.com/company/profile/samplecompany/services
http://mainwebsite.com/samplecompany/services

http:// mai nwebsite.com/company/profile/samplecompany/contactus
http://mainwebsite.com / samplecompany / contactus


其中
samplecompany`是动态部分
是否可以使用以下格式创建网站链接?

wheresamplecompany` is the dynamic part Is it possible to create site links in the format?


  1. 我了解使用给定域的A记录,我可以指出一个域, http://www.samplecompany.com http://mainwebsite.com/company/profile/samplecompany ,因此键入 http://www.samplecompany.com ,则应将其带到 http:// mainwebsite。 com / company / profile / samplecompany 。如果成功实现,
    http://www.samplecompany.com/feedback
    http://www.samplecompany.com/services
    http:// www .samplecompany.com / contactus

  1. I understand using A record for a given domain, I can point a domain say, http://www.samplecompany.com to http://mainwebsite.com/company/profile/samplecompany so typing http://www.samplecompany.com he should be taken to http://mainwebsite.com/company/profile/samplecompany . If this is successfully implemented, will http://www.samplecompany.com/feedback http://www.samplecompany.com/services http://www.samplecompany.com/contactus

工作正常吗?

推荐答案


我想可以创建具有/获取 http:// mainwebsite.com/samplecompany 使用htaccess和默认控制器。如果有人可以帮助htaccess规则,那就太好了。我已经在使用htacess从CI中删除index.php,但无法正常工作。
给定用户/公司的其他页面将很少,例如反馈,联系我们,服务等。因此,我想到的实现链接的格式为< http://mainwebsite.com/company/profile/samplecompany/feedback http://mainwebsite.com/samplecompany/feedback

I guess it is possible to create to have/get http://mainwebsite.com/samplecompany using htaccess and a default controller. If anybody can help with the htaccess rule , that would be awesome. I am already using htacess to remove index.php from CI but could not get this working. There will be few other pages for the given user/company such as feedback, contact us, services etc. So the implementation links that come to my mind is of the form ` http://mainwebsite.com/company/profile/samplecompany/feedback or http://mainwebsite.com/samplecompany/feedback

您可以使用路线完成此操作。例如,在您的 /config/routes.php 文件中,输入以下内容:

You can accomplish this using routes. For example, in your /config/routes.php file, put this:

$route['samplecompany'] = "company/profile/samplecompany";
$route['samplecompany/(:any)'] = "company/profiles/samplecompany/$1";

第一个规则告诉CodeIgniter,当有人访问 http://mainwebsite.com/samplecompany ,它应该像处理URL一样是 company / profile / samplecompany一样对其进行处理。第二条规则捕获URI字符串中 samplecompany之后的所有内容,并将其附加到末尾。

The first rule tells CodeIgniter that when someone accesses http://mainwebsite.com/samplecompany that it should process it as if the URL were "company/profile/samplecompany". The second rule captures anything that comes in the URI string after "samplecompany" and appends it onto the end.

但是,如果您有多个公司(而不仅仅是samplecompany),除非每次添加新公司时都要手动编辑配置文件,否则您可能要扩展CI的路由器以支持此设置。

However, if you have multiple companies(not just samplecompany), you're probably going to want to extend CI's router to suppor this unless you want to manually edit the config file each time a new company is added.

好的,您肯定要处理动态的公司名称(根据您的评论)。这有点棘手。我无法提供完整的代码,但是可以为您指明正确的方向。

OK, you're definitely going to want to handle dynamic company names(as per your comment). This is a little trickier. I can't give you the full code, but I can point you in the right direction.

您需要扩展CI的路由器,并在传入请求中查询DB用于公司名称列表。如果URI细分与公司名称匹配,则需要使用 company / profile 方法为其提供服务。如果不是,您将忽略它并让CI正常处理它。查看有关此主题的更多信息:论坛链接

You'll want to extend CI's router and on an incoming request query the DB for the list of company names. If the URI segment matches a company name, you'll want to serve it up using your company/profile method. If it does not, you will ignore it and let CI handle it normally. Check out this post on this topic for more information: forum link.

这篇关于使用htaccess在Codeigniter中的自定义URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆