Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mod rewrite Mod重写CodeIgniter中的新手_Mod Rewrite_Codeigniter_Url Rewriting - Fatal编程技术网

Mod rewrite Mod重写CodeIgniter中的新手

Mod rewrite Mod重写CodeIgniter中的新手,mod-rewrite,codeigniter,url-rewriting,Mod Rewrite,Codeigniter,Url Rewriting,我对mod重写还很陌生,但我很难做到这一点: 我希望和都重定向到,而不更改浏览器中访问者的url 目前,my.htaccess看起来像这样,用于从URL中删除www <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{HTTP_HOST} ^www.myurl.com [NC] RewriteRule ^(.*)$ http://myurl.com/$1 [L,R=301] </IfModu

我对mod重写还很陌生,但我很难做到这一点:

我希望和都重定向到,而不更改浏览器中访问者的url

目前,my.htaccess看起来像这样,用于从URL中删除www

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.myurl.com [NC]
RewriteRule ^(.*)$ http://myurl.com/$1 [L,R=301]
</IfModule>

重新启动发动机
重写基/
重写cond%{HTTP_HOST}^www.myurl.com[NC]
重写规则^(.*)$http://myurl.com/$1[L,R=301]
我尝试了许多不同的方法来重定向这个特殊的URL,但到目前为止,没有任何方法对我有效,每种方法都会产生不同的不良结果。请让我知道,如果你有什么想法,我可以添加在这里

我试过这个:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^special$ /index.php/special$1 [NC]
RewriteCond %{HTTP_HOST} ^www.myurl.com [NC]
RewriteRule ^(.*)$ http://myurl.com/$1 [R=301]
</IfModule>

重新启动发动机
重写基/
重写规则^special$/index.php/special$1[NC]
重写cond%{HTTP_HOST}^www.myurl.com[NC]
重写规则^(.*)$http://myurl.com/$1[R=301]

这将重定向到默认控制器,并重定向到浏览器中的url并对其进行更改。这两种方法都不完全正确,尽管第二种方法更接近我的需要。

假设www.myurl.com和myurl.com都被设置为VirutalHost中的服务器别名,那么简单的方法就是

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

重新启动发动机
重写基/
重写规则^(.*)$index.php/$1[L]

哦,不,你不需要htaccess,只要使用CI的路由就可以了。在此,首先将您的Htaces更改为:

# Customized error messages.
ErrorDocument 404 /index.php

# Set the default handler.
DirectoryIndex index.php

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
你的网址不会改变,一切都会很完美

但是不要停止阅读,只是为了确定一下

这就是您完成以下操作后的路线:

//these two are for CI 
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";

//this is your route sending all and any traffic for special
//no matter how many levels it will always go to welcome->index.
$route['special/(:any)'] = "welcome";

//if you want to use sub-pages or other functions within your welcome controller 
//i.e. welcome->signup. then just send the route information to the controller like this:
$route['special/(:any)'] = "welcome/$1";
php中的路由类功能非常强大,这只是其中的一部分,有关路由的更多详细信息,请参见本页:


真希望我没有把你弄糊涂。祝你好运

嘿,布兰登,谢谢你的快速回复。我之所以有这样的.htaccess文件,是为了让所有请求都重定向到它。我需要的是让它继续,但除了所有的请求都要重定向到。在这方面你还能提供更多的帮助吗?我试着按照你的建议修改htaccess和routes文件,但没有成功。myurl.com/special URL得到了一个404 Not Found页面。我意识到我没有像你提到的那样设置htaccess,我试过了,但是现在任何没有index.php的URL(比如myurl.com/special)都只显示默认的主控制器。知道为什么会这样吗?使用htaccess,它可以对同一个页面同时执行myurl.com/index.php/special和myurl.com/special,从而使URL更干净。但是我刚刚意识到发生了什么我很抱歉我没有读对只是删除你添加的路线,它将完美地工作我很抱歉我以为你想让myurl.com/special转到默认页面htaccess会一直这样做我很抱歉我应该仔细读它离开htaccess,只是删除路线你补充道。让我知道这是否有效。我只是仔细检查了一下,这应该对您有效,但请确保如果您使用base_url()CI项,您已经正确地将base_url配置项设置为尾随/并且index.php位于$config['index_page']=“index.php”下的配置中如果您没有设置这些url,并尝试使用为您构建url的任何CI函数,则这些url将不起作用。不幸的是,它仍然不起作用。它将所有请求重定向到主控制器,而不是正确的控制器。
//these two are for CI 
$route['default_controller'] = "welcome";
$route['scaffolding_trigger'] = "";

//this is your route sending all and any traffic for special
//no matter how many levels it will always go to welcome->index.
$route['special/(:any)'] = "welcome";

//if you want to use sub-pages or other functions within your welcome controller 
//i.e. welcome->signup. then just send the route information to the controller like this:
$route['special/(:any)'] = "welcome/$1";