Reputation: 21
I've done a lot of searching, done a few tutorials, I think I know how it's supposed to work, but it ain't working. Anybody know what's up with rewriteRule in CakePHP? Are there "issues" with it?
By the way, I started out trying to use Router::connect and couldn't get that happening either. I'll happily accept either solution.
The goal: to route or redirect my clumsy old query strings (?action=show&id=3) to groovy CakePHP URLs (/show/3), e.g., so that people's existing bookmarks still work once I complete my switch-over to Cake. All I really need to capture is the digits from the id= bit.
Here's my latest attempt at a rewriteRule (.htaccess in webroot):
RewriteRule id=([0-9]+)$ /Features/view/%1 [R=301,L]
Does precisely nothing.
Here's my latest attempt in Router::connect:
Router::connect(
'index.php?action=show&id=:id',
array('controller' => 'features', 'action' => 'view'),
array('pass'=>array('id'),'id' => '[0-9]+')
);
Does nothing whatsoever. I've now spent three entire long evenings reading, trying things, and failing. I am ready for your help.
Upvotes: 0
Views: 201
Reputation: 60453
I can't help you with the router since I've never used non-pretty URLs with CakePHP, however I can tell you how it should work using mod_rewrite
.
The problem with your rewrite attempt ist that RewriteRule
matches only the path part of the URI, if you want to match the query string, then you have to use RewriteCond
with the %{QUERY_STRING}
variable, something like this:
RewriteCond %{QUERY_STRING} id=(\d+)$
RewriteRule ^.*$ /Features/view/%1? [R=301,L]
Don't forget the ?
at the end on the rewrite rules substitution part, this ensures that the original query string doesn't get appended, in this case it would lead to an infinite loop, because of the very lax query and path matching rules. You might want to tigthen the rules a little more, something like:
RewriteCond %{QUERY_STRING} ^action=show&id=(\d+)$
RewriteRule ^index\.php$ /Features/view/%1? [R=301,L]
However this will of course depend on your specific URIs, so see this as a basic starting point.
Upvotes: 1