1

Learning PHP, I am playing around with mod_rewrite and CodeIgniter. I have configured my .htaccess file correctly with

RewriteEngine On
RewriteRule ^(resources)/(.*) $1/$2 [L]
RewriteRule ^(user_guide)/(.*) $1/$2 [L]
RewriteRule (.*) index.php?$1 [L]

I understand a bit of regex, and can appreciate what happens here. The rewrite rules are applied and the server than handles the final URL which in the above case- attaches index.php (the front controller) to the "pretty" URL. So far so good.

I now want a URL pattern :

/<person-name>/at/<place>

to get translated to :

/index.php/person/list?personName=$1&place=$2

And i handle the request at my list function in the person controller. I do not understand why the following doesn't work:

RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php/person/list?personName=$1&place=$2 [L]

What am i doing wrong/where is my understanding flawed? I see that the placeholders are extracted correctly ($1 and $3), however, it throws a CodeIgniter 404.

Many thanks!

Chinmay
  • 4,726
  • 6
  • 29
  • 36
  • 1
    could you not do in the `routes.php - `$route['([a-z]+)/at/([a-z]+)'] = "person/list/$1/$2"; – Pattle Mar 08 '14 at 22:43
  • Yes I did that eventually when i learnt about the routes.php. :-) I'm looking for an answer as to why i cannot do that at the .htaccess file. :-) Btw, why can't we access those $1 and $2 via the GET params? Does that work? – Chinmay Mar 09 '14 at 04:50
  • In your `config.php` are there both set to true? - `$config['allow_get_array'] = TRUE; $config['enable_query_strings'] = TRUE;` – Pattle Mar 09 '14 at 09:51
  • enable_query_strings wasn't but i just made it TRUE and checked. Same story. – Chinmay Mar 09 '14 at 10:46

1 Answers1

1

It's possible that the simplest fix will fix your issue. By wrapping "at" in parentheses, you're creating another matching group, which means that $2 will always be "at." That could be breaking everything. You want index.php?person/list?personName=$1&place=$3 But you may have noticed that issue and fixed it without fixing the problem, in which case, read on.

Check out How to make CodeIgniter accept "query string" URLs?. It seems to indicate that you can't mix and match the segment-based approach and the query string approach. Without seeing your controller code, I can't say for certain, but I'd start investigating there. You might also try:

RewriteRule ^([a-z]+)/(at)/([a-z]+)$ index.php?person/list/$1/$3 [L]

which would do the same thing the general-purpose CI redirect rule below does; send the URL off to index.php as a query string for processing. You've said you got it working with routes, so rather than passing a query string to your controller, you can expect person and place as two arguments. CI's handling of query strings leaves a lot to be desired, and I've never tried to MOD_REWRITE something including a query string into a query string.

Community
  • 1
  • 1