Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial

Abhay Sharma
8,287 PointsRedirction Loop Error
I am trying to re-write the url with a query string of page=# with the below mentioned code
RewriteRule ^admin/admin-view/$ /admin/admin-view.php
RewriteRule ^admin/admin-view/([0-9]+)/$ /admin/admin-view.php?page=$1
So far the above code works fine but when I add the Regex to redirect the old syntax to new one I get an error of redirection loop
RewriteCond %{QUERY_STRING} ^page=([0-9]+)$
RewriteRule ^admin/admin-view.php$ /admin/admin-view/%1/? [R=301]
1 Answer

Randy Hoyt
Treehouse Guest TeacherThere's another flag for rewrite rules that you need, an [L]. This means the rule is that last rule that should be applied. I think that's what you need after the second rule:
RewriteRule ^admin/admin-view/$ /admin/admin-view.php
RewriteRule ^admin/admin-view/([0-9]+)/$ /admin/admin-view.php?page=$1 [L]
RewriteCond %{QUERY_STRING} ^page=([0-9]+)$
RewriteRule ^admin/admin-view.php$ /admin/admin-view/%1/? [R=301]
Without that, here's what happens.
- Someone visits [admin/admin-view/2/]
- That URL gets rewritten to [admin/admin-view.php?page=2] because of the second rule.
- The third line (the RewriteCond) then kicks in because the URL now (after getting rewritten) has a page variable.
- The fourth line redirects the newly rewritten URL of [admin/admin-view.php?page=2] back to [admin/admin-view/2/], and the whole thing starts again.
This causes an endless redirect problem. With that [L] after the second rule, the page will load properly without getting redirected.
I typically put the redirects like you have in lines 3-4 before the rewrites, just to make sure to avoid that.
Does that help?
Abhay Sharma
8,287 PointsAbhay Sharma
8,287 PointsThanks for the reply Randy. It still is giving me redirection loop error. Now I have:
The new url is redirected fine. The problem arises when I add the line 2 and 3 so that to redirect the old url's /aims/admin-view.php?page=2 which does changes to aims/admin-view/2/
Randy Hoyt
Treehouse Guest TeacherRandy Hoyt
Treehouse Guest TeacherHa! Now you need an L on the new line 3, also. I think changing it to this should work:
Abhay Sharma
8,287 PointsAbhay Sharma
8,287 PointsNope that did not worked out either. Could there be any problem because of line 1 as my redirection is first from admin-view.php to adminview/ then admin-view.php?page=2 to admin-view.php/2/ and I want to execute admin-view.php?page=2 when someone search for admin-view.php/2/ . I know the last two logics contradicts. But is there a way to say execute this and redirect this.