Codeignitor .htaccess redirection for query string URL
A single URL redirect is as simple as adding the following line in your .htaccess file (only if mod_alias module is installed on your Apache server):
Another simple way of specifying the redirection would be to add a route in application/config/routes.php file. You need to provide the name of controller and the method for the URL you want the redirection to happen.
However, the same doesn't work well with the query based string URLs. Following redirect statements containing query strings are simply ignored as they are not valid.
To get it working, we need to rewrite the URL instead of redirecting it. For this we need 'mod_rewrite' module to be installed on your Apache server.
"RewriteEngine On" enables the URL rewrite capability. RewriteCond are the conditions which must be true, to get the URL rewrite triggered. Here we check if the URL contains "/articles/view_article.php" and the query string is "id=<some integer>", our RewriteRule will be called. The rewrite rule will change the matched pattern (whole URL in this case) to the one we want - "http://edusagar.com/articles/view/<some integer>". The "%1" maps to the integer that matched the pattern in the RewriteCond for query string, i.e. ^id=([0-9]*)$.
"R=301" states that the server will return 301 response code to the browser and search engines. Along with this 'L' here specifies that this will be the last rule to execute if all the RewriteCond matched properly.
Once you are done with this, "/articles/view_article.php?id=1" will be successfully redirected to "http://edusagar.com/articles/view/1?id=1".
Is something missing? By default, the original query string gets passed on to the rewritten URL. To discard the original query string append a '?' at the end of the URL in the rule.
Hope that gets you get going...