views:

26

answers:

3

I'm in the midst of moving a site from "plain old" php to the CakePHP framework and I need to be able to handle some legacy URLs, for example:

foo.com/bar.php?id=21

is the pattern of the current URLs, but in the new site using cake, that URL needs to redirect to:

foo.com/blah/21

So basically, I need to be able to grab that ID number and pass it along.

It may be that I'm working on about 4 hours sleep, but I can't seem to get it to work in /app/webroot/.htaccess no matter what regex pattern I get.

I'm open to any solution that uses the .htaccess or routes.php - either way I'd be grateful for ideas!

+1  A: 

Try to debug

Nik
That is very handy. Having some debugging ability will definitely help!
Dave Simon
A: 

I think you'll probably find a solution here: http://www.simonecarletti.com/blog/2009/01/apache-query-string-redirects/, using something like:

RedirectMatch ^/oldfolder/(.*)$ http://mydomain.site/newfolder/$1

It can be difficult to get your head round htaccess - I don't have much space left in my head - but it is worth persevering with the apache manual and looking at some of the hundreds of thousands of tutorials and solutions that Google will return.

Leo
This link helped _a lot_ as it had 90% of the answer I neeeded in this: `RewriteEngine On``RewriteCond %{QUERY_STRING} ^id=([0-9]*)$``RewriteRule ^page\.php$ mydomain.site/page/%1.pdf [R=302,L]`Just some minor tweaks to this and I should have the right solution. I'm surprised I never got that page with all of my googling. But with some sleep, some 5-hour Energy and this link, I think I'm about there
Dave Simon
A: 

So with a little tweaking, the final rewrite turned out like so:

RewriteEngine On
# My hack to get Legacy URLs to work
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^bar\.php$ http://foo.com/bar/%1? [R=301,L]
# Cake's Default Rewrite
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

Note the trailing ? after the %1 - without it, the rewrite is foo.com/bar/21?id=21, with it, it's foo.com/bar/21

Dave Simon