使用 htaccess 在 Codeigniter 中自定义 URL
我正在 Codeigniter 2.0.2 上开发一个网站。在该网站上,公司/用户可以注册并创建自己的个人资料页面,拥有自己的自定义 URL(例如 http://facebook. com/demouser),有自己的反馈系统,展示自己的服务。
这就是说,我已经成功地以以下格式显示个人资料页面
http://mainwebsite.com/company/profile/samplecompany
这将显示公司 samplecompany
的主页,其中 company 是控制器,profile 是方法。
现在我有几个问题,
我想可以创建 has/get http://mainwebsite.com/samplecompany 使用 htaccess 和默认控制器。如果有人可以帮助解决 htaccess 规则,那就太棒了。我已经在使用 htacess 从 CI 中删除 index.php 但无法正常工作。
对于给定的用户/公司,将有一些其他页面,例如反馈、联系我们、服务等。因此,我想到的实施链接的形式是 ` http://mainwebsite.com/company/profile/samplecompany/feedback 或 http://mainwebsite.com/samplecompany/feedback
http://mainwebsite.com/company/profile/samplecompany/services 或 http://mainwebsite.com/samplecompany/services
http://mainwebsite.com/company/profile/samplecompany/contactus 或 http://mainwebsite.com/samplecompany/contactus
其中
samplecompany` 是动态部分 是否可以创建该格式的站点链接?
- 我了解对给定域使用 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
工作正常吗?
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
This displays the home page for the company samplecompany
, where company is the controller and profile is the method.
Now I have few questions,
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
http://mainwebsite.com/company/profile/samplecompany/services or
http://mainwebsite.com/samplecompany/services
http://mainwebsite.com/company/profile/samplecompany/contactus or
http://mainwebsite.com/samplecompany/contactus
samplecompany` is the dynamic part
where
Is it possible to create site links in the format?
- 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
work correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 路线。例如,在您的
/config/routes.php
文件中,输入以下内容:第一条规则告诉 CodeIgniter,当有人访问 http://mainwebsite.com/samplecompany 它应该像 URL 是“company/profile/samplecompany”一样处理它。第二条规则捕获 URI 字符串中“samplecompany”之后出现的任何内容,并将其附加到末尾。
但是,如果您有多个公司(不仅仅是示例公司),您可能需要扩展 CI 的路由器来支持这一点,除非您想在每次添加新公司时手动编辑配置文件。
好的,您肯定会想要处理动态公司名称(根据您的评论)。这有点棘手。我无法给您完整的代码,但我可以为您指出正确的方向。
您需要扩展 CI 的路由器,并根据传入请求在数据库中查询公司名称列表。如果 URI 段与公司名称匹配,您将需要使用
company/profile
方法来提供它。如果没有,您将忽略它并让 CI 正常处理。查看有关此主题的这篇文章以了解更多信息:论坛链接。You can accomplish this using routes. For example, in your
/config/routes.php
file, put this: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.
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.
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.以下是有关如何实现您的需求的精彩指南:Codeigniter Vanity URL。
Here's a great guide on how to achieve what you need: Codeigniter Vanity URL's.