更改codeigniter中的网址 [英] change url in codeigniter

查看:84
本文介绍了更改codeigniter中的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像

的网址

  mysite.com/index.php/home/user_get 



这样工作正常。



URL中的哈希字符

  mysite.com/username 

有没有反正在代码igniter允许我使用这种类型的url我登录?

解决方案

要摆脱index.php前缀,你必须在你的apache配置文件中启用mod_rewrite,并使用htaccess重写url。



要启用mod_rewrite,请打开httpd.conf文件并取消注释以下行:

  LoadModule rewrite_module modules / mod_rewrite.so 

要重写URLS,必须将.htaccess文件(命名为.htaccess)放入您的网站根目录并添加以下内容

  RewriteEngine On 
RewriteCond $ 1!^(index\.php | images | scripts | css | robots\.txt)
RewriteRule ^(。*)$ /index.php/$1 [L]


b $ b

第三行很重要,因为它允许静态内容提供。您必须修改它以匹配您的项目结构。



我认为不可能使用户依赖。
请参见 mod_rewrite文档以供参考



UPDATE:
如果只是关于使用 mysite.com/username ,而不是 mysite.com/index.php/home/user_get 您可以使用单一方法

$ b定义 username
$ b

  public function index(){
redirect('/ home / user_get','refresh');
}

如果您要支持多个用户, mysite.com/john,mysite.com/dave,可能有帮助

  RewriteEngine On 
RewriteCond $ 1 ^ \.php | images | scripts | css | robots\.txt)
RewriteRule $ 1 [L]

RewriteCond $ 1!^(products | categories)
RewriteRule ^ 。*)$ /index.php/users/$1 [L]

RewriteRule ^(。*)$ /index.php/$1 [L]

这将路由任何请求到未知的控制器到用户控制器。您必须在(products | categories)中列出所有控制器。在用户控制器中,您将有用户名作为参数,只要你的URL是/ users / username。请注意,这未经过测试。


I have a url that looks like

mysite.com/index.php/home/user_get

which works fine.

however my client now wishes to have a hashbang in the url

mysite.com/username

Is there anyway to in code igniter allow me to use this type of url whren i logged in?

解决方案

To get rid of index.php prefix you'll have to enable mod_rewrite in your apache config file and use htaccess to rewrite url.

To enable mod_rewrite, open httpd.conf file and uncomment the following line

LoadModule rewrite_module modules/mod_rewrite.so

To rewrite URLS you have to place .htaccess file (named exactly '.htaccess') into your site root and add the following lines to it:

RewriteEngine On
RewriteCond $1 !^(index\.php|images|scripts|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Third line is important, as it allow static content to be served. You'll have to modify it to match your project structure.

Making user-dependent is impossible, I think. See mod_rewrite documentation for reference

UPDATE: If it is just about usning mysite.com/username instead of mysite.com/index.php/home/user_get you could define username controller, with single method

public function index(){
    redirect('/home/user_get', 'refresh');
}

If you going to support several users, e.g. mysite.com/john, mysite.com/dave, that might help

RewriteEngine On
RewriteCond $1 ^(index\.php|images|scripts|css|robots\.txt)
RewriteRule $1 [L]

RewriteCond $1 !^(products|categories)
RewriteRule ^(.*)$ /index.php/users/$1 [L]

RewriteRule ^(.*)$ /index.php/$1 [L]

this will route any requests to unknown 'controller' to users controller. You'll have to list all your controllers in (products|categories). In the users controller you will have username as parameter, just if your url would be /users/username. Please note that this is not tested.

这篇关于更改codeigniter中的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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