2

When I first launched my site, URLs were in the following format:

project.php?projectID=1&pageID=2

A few years ago I modified my .htaccess to rewrite these to use segments, like this:

project/1/2

I updated all internal links to use the segmented format, but this was good because it still supported any external incoming links in the old format.

Now I have switched to CodeIgniter, having my pages render using the segmented format is easy, but I'm struggling to work out how to support the old query-string URLs.

My attempts to support query strings natively in CodeIgniter seem to break the segmented URLs, so it looks like I cannot have both supported simultaneously. Setting up a route in CodeIgniter to redirect a query string to a controller doesn't seem to work. E.g.:

$route['project.php?projectID=(:any)&pageID=(:any)'] = 'home/projectview/$1/$2';

My second thought was to modify the .htaccess directly to redirect the query string to the controller, like so:

(Note I am also using the .htaccess rewrite that eliminates the appearance of index.php in the CI URL)

RewriteRule ^project.php?projectID=(\d*)&pageID=(\d*) project/$1/$2/ [L]
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]

However this rewrite rule doesn't appear to work either. It redirects to my CodeIgniter 404 without being rewritten, and even by removing the default index.php rule it doesn't redirect, so I'm assuming there is something in my regex/.htaccess formatting here that is incorrect.

1 Answer 1

2

You need to match against the request and not the URI. The URI won't have any of the query string in it:

RewriteCond %{THE_REQUEST} \ /+project\.php\?projectID=([0-9]+)(?:&pageID=([0-9]+)|)
RewriteRule ^ /project/%1/%2? [L,R=301]

RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php/$1 [L]
Sign up to request clarification or add additional context in comments.

2 Comments

This works great, thank you, but could I ask for one revision? I also accommodate the '&pageID=' being absent (it defaults to page 1). How do I make this part of the regex optional?
Hi @JonLin.. i have a bit similar issue can you help me out with this.. https://www.jesvenues.com/home/search/?location=Hyderabad&occasion=Anniversary&gathering=100&date=29-Sep-my URL is 2017&timing=Lunch and i have tried your solution for this to get it as https://www.jesvenues.com/search/Hyderabad/Anniversary/100/29-Sep-2017/Lunch.. but its not working... can you please help me out with this..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.