简体   繁体   中英

CodeIgniter .htaccess / Mod_rewrite

In the past I have solved this problem by creating a new rule for every single controller. So here we go; By default the url is as below;

http://www.url.com/index.php/controller/method/parameters

I want to change this to;

http://www.url.com/controller/method/parameter1/parameter2/parameter3/etc...

My problem is the parameters-part as I sometimes have 3 or 4 parameters, but sometimes none or just one.

Right now I have this (which does not work, otherwise I wouldn't post):

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

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

If I request just the controller it works (since an index() method is present), but custom methods and parameters do not work.

Then, do I also have to add routes to the config/routes.php? In the past I fixed this problem by adding custom routes and rules for every single used combination.

I google'd for days, no success. Please tell me if I missed a post.

Thanks in advance.

Your best bet is to change your URL structure, this will require the least amount of configuration:

Use a structure like http://www.url.com/controller/method/param1_key/param1_value/param2_key/param2_value/etc...

Then, you can use $this->uri->uri_to_assoc() to get the key => value pairs in an associative array.

See the docs here: http://codeigniter.com/user_guide/libraries/uri.html

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

That does the trick for me (note ? instead of / ), using $config['uri_protocol'] = 'REQUEST_URI'; in the config file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM