开发者

Legacy URLs in CakePHP using routes or htaccess

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开发者_StackOverflow中文版 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!


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜