htaccess rewrite URL does not work with codeignite

2020-05-08 07:40发布

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

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

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] 

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?

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

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.

1条回答
Viruses.
2楼-- · 2020-05-08 08:04

Take a look at https://gist.github.com/keithmorris/3023560

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.

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

查看更多
登录 后发表回答