Override rewrite rules in .htaccess in CodeIgniter

Viewed 29

I am adding some features to an existing web site which was created using CodeIgniter.

We generally use CI's default routing but the former developer of the site, as I see the code, tried to reinvent the wheel.

Usually CI thinks that the routes first parameter is the controller name and the second after slash is function name and trailing parameters are function parameters. But this guy probably didn't know that and the web site is a total mess now. Especially routing logic is awful.

His .htaccess file is as the following:

RewriteEngine on 

DirectoryIndex index.php

RewriteRule ^google[0-9a-f]+.html$ - [L]

RewriteRule ^(shop.html)$   index.php?mod=shop&name=$1 [L,NC]

RewriteRule ^(ref.html)$   index.php?mod=ref&name=$1 [L,NC]

RewriteRule ^(refs.html)$   index.php?mod=aff&s=refs&ad=$2  [L,NC]

RewriteRule ^(vip-membership.html)$   index.php?mod=aff&s=top [L,NC]

RewriteRule ^(dds.html)$   index.php?mod=aff&s=dds [L,NC]

RewriteRule ^(news.html)$   index.php?mod=news [L,NC]

RewriteRule ^(document)-(.*)-(.*).html$   index.php?mod=aff&s=document&id=$3 [L,NC]

RewriteRule ^(category)-(.*)-(.*).html$   index.php?mod=aff&s=category&top=$3 [L,NC]

RewriteRule ^(news)_(.*)_(.*).html$   index.php?mod=news&s=detail&name=$1&id=$2 [L,NC]

RewriteRule ^(.*)_news.html$   index.php?mod=news&s=category&catname=$1&id=$2 [L,NC]

RewriteRule ^(page)-(.*).html$   index.php?mod=aff&s=page&name=$2 [L,NC]

RewriteRule ^([a-z0-9_-]+).html$   index.php?mod=page&name=$1 [L,NC]

RewriteRule ^([a-z0-9_-]+).html$   index.php?mod=page&name=$1 [L,NC]


 

RedirectMatch 301 ^/panel$ /index.php/panel

RedirectMatch 301 ^/2017-books.html$   /dhtc-2017-books.html




php_value max_input_vars 3000
php_value suhosin.get.max_vars 3000
php_value suhosin.post.max_vars 3000
php_value suhosin.request.max_vars 3000

What I want to do is to provide seo friendly urls like controller_name/function_name/$param1/$param2.

I want this .htaccess to work as is, but also it should allow me to write my own urls. For example

RewriteRule ^([a-z0-9_-]+).html$   

will do its own work but will not interfere my urls like

$route['download/(:any)/(:any)/(:any)'] = '/controller/download/$1/$2/$3';

I have some routes in routes.php. I don't think they are taken into account.

There's a download method in my controller gets 3 parameter:

  • $1: folder name,
  • $2: file id and
  • $3: file name

Preventing any confliction how can I add new rules to this .htaccess?

1 Answers

I think you are just missing the standard front-controller pattern... something to actually rewrite URLs of the form controller_name/function_name/$param1/$param2 to the CI front-controller (ie. index.php). Otherwise, a request like that will just fall through to an Apache 404, since there is nothing to handle the request (none of the existing rules would match such a request).

For example, try adding the following after the last RewriteRule directive (and before the RedirectMatch directive).

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [L]

This passes the URL-path as the query string to index.php. Depending on how CI is configured, you may need to pass this as PATH_INFO instead (ie. omit the ?). For example:

:
RewriteRule (.*) index.php/$1 [L]

Or simply remove the trailing /$1 altogether and let CI use PHP's $_SERVER['REQUEST_URI']. For example:

:
RewriteRule . index.php [L]

Any of the existing URLs that match the former rules will take priority.

Related