htaccess重写URL不适用于codeigniter代码 [英] htaccess rewrite URL does not work with codeigniter code

查看:92
本文介绍了htaccess重写URL不适用于codeigniter代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试重写URL,以便/ foo-> index.php。

I am trying to rewrite URLS so that /foo -> index.php.

index.php由codeigniter 3驱动。
bar.php是一个独立的php文件。

index.php is powered by codeigniter 3. bar.php is a stand alone php file.

这是从我的.htaccess中截取的内容:

This is a snipped from my .htaccess:

//Test 1. Bar.php displayed. This works as exptected. 
RewriteRule ^/?foo1/?$ /bar.php [QSA,L] 


//Test 2. Redirects succesfully to index.php. Works as expected.
RewriteRule ^/?foo2/?$ /index.php [R=301,QSA,L]    


//Test 3. Goes to codeigniter 404 page and does NOT display the homepage. 
Does not work as expected
RewriteRule ^/?foo3/?$ /index.php [QSA,L] 

为什么测试3不能按预期显示?尝试重写URL时,我的代码初始化器代码中有些功能不起作用。任何想法如何解决这一问题?有解决方法吗?

Why does Test 3 not display as expected? There is something in my codeigniter code that doesn't work when trying to rewrite URLS. Any ideas how to fix this? Is there a workaround?

全面披露:长期目标是要拥有一个结构如下的URL:
example.com/foo1/ foo2 / foo3 /?query1 = xxxx& query2 = yyyy
重写为
example.com/bar/bar1.php?queryA=foo2&queryB=foo3&query1=xxxx&query2=yyyy

Full disclosure: the long term aim is to be able to have a URL with structure like this: example.com/foo1/foo2/foo3/?query1=xxxx&query2=yyyy rewrite to example.com/bar/bar1.php?queryA=foo2&queryB=foo3&query1=xxxx&query2=yyyy

是否可以在codeigniter框架(route.php)而非htaccess中轻松完成此操作?从开发时间的角度来看,仅希望通过.htaccess使其起作用。

Can this be "easily" done within the codeigniter framework (route.php) rather than htaccess? From a development time perspective, just getting it to work via .htaccess would be the preference.

推荐答案

看看 https://gist.github.com/keithmorris/3023560

这包括我在所有项目中使用的相当标准的htaccess文件。然后,您可以使用CI路由功能来定义不同位的位置。

This includes a fairly standard htaccess file that I use on all my projects. You then use the CI routing functionality to define where different bits go.

请记住,使用CI时,您可以在网址中传递 additional 元素,例如例如example.com/foo1/foo2/foo3/query1/query2。

Bear in mind with CI you can pass additional elements in a url such as example.com/foo1/foo2/foo3/query1/query2.

这篇关于htaccess重写URL不适用于codeigniter代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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