I am building a blog-ish site using CI. I am using the HMVC plugin. The module that I am working in is “/journal”.
The individual articles are accessed at /journal/article/ID/SLUG. This works fine, but I would like to shorten the URI to /journal/ID/SLUG using mod_rewrite.
Here are my rules:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^journal/([0-9]+)(.*)$ index.php?/journal/article/$1$2 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1
For testing I am using /journal/2.
I know that the rules is matching. If, for instance, I change the redirect to http://google.com, I will indeed get redirected to Google. However, when using the rule as written it seems to be ignored and I get a 404 no matter what I put in.
Am I making some obvious (or arcane) error?
Edit: I figured this out shortly after posting the question. My rules are indeed correct but I need to change the following line in config/config.php:
$config['uri_protocol'] = 'AUTO';
to
$config['uri_protocol'] = 'PATH_INFO';
I won’t claim to know exactly what that change does or why it fixes the issue. Perhaps someone can follow up with an explanation.
The $config[‘uri_protocol’] tells CI which $_SERVER superglobal to use to determine your apps URI. The ‘PATH_INFO’ option uses $_SERVER[‘PATH_INFO’] which is the URL request (without host portion), see php manual.
The ‘AUTO’ option is a CI thing to make suitable for different environments without config tweaks.
Personally, I have written a few PHP SEF controllers, I find it better to do all the processing with the PHP controller(s) scripts.
htaccess and rewrites can be tricky, harder to debug and one typo can kill the whole site (ouch). I am sure there are small performance gains, but one would need some pretty heavy demands. You are heading to your index.php controller anyway. I find happier code when it is all in one place 😉
good luck with it…and hopefully I provided some insight to your issue.