I'm looking to get URL spinning to operate on my small website. This is actually the items in my .htaccess:

RewriteEngine On
RewriteRule ^blog/?$ index.php?page=blog [L]
RewriteRule ^about/?$ index.php?page=about [L]
RewriteRule ^portfolio/?$ index.php?page=portfolio [L]
#RewriteRule ^.*$ index.php?page=blog [L]

The 3 uncommented rewrite rules work perfectly, basically try http://www.mysite.com/blog/, I recieve rerouted to http://www.mysite.com/index.php?page=blog, exactly the same for "about" and "portfolio". However, basically mistype blog, say I attempt http://www.mysite.com/bloh/, then clearly I recieve a 404 error. The final rule, the said one, ended up being to assist in preventing that. Any URL should get rerouted towards the blog, however this rule continues to be parsed even when we've effectively used an earlier one, and so i used the "last" flag ([L]). Basically uncomment my last rule, anything, including blog, about, and portfolio, redirect to blog. Should not the "last" flag steer clear of the execution the moment it finds an identical rule?


Yes, the final flag means it will not apply the rules after this rule within this request.

After spinning the URL, it can make an interior request while using new rewritten URL which may suit your last RewriteRule and therefore your redirects get into an infinite loop.

Make use of the RewriteCond directive to limit spinning to Web addresses that do not begin with index.php, and you ought to be fine.

You could include an ailment like:

RewriteCond %{REQUEST_URI} !^index\.php

I'll also point out that using RewriteRule ^.*$ is a great way to break all your media demands (css, js, images) too. You might like to then add conditions like:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

To make certain you are not attempting to rewrite actual files or sites which exist in your server. Otherwise they will be unreachable unless of course index.php serves individuals too!

From apache's mod_rewrite docs

'lastL' (last rule) Steer clear of the spinning process here and do not apply anymore rewrite rules. This matches the Perl last command or even the break command in C. Make use of this flag to avoid the presently rewritten URL from being rewritten further by using rules. Remember, however, when the RewriteRule creates an interior redirect (which regularly happens when spinning inside a per-directory context), this can reinject the request and may cause processing to become repeated beginning in the first RewriteRule.

You could utilize

 ErrorDocument 404 /index.php?page=blog

but you should know to the fact that it does not return 404 error code, but a redirect one and I'm not sure if that's such a sensible practice.

Once you [L]eave processing for that request, the entire processing runs again for that new (rewritten) URL. You can get from that loop applying this before your other rules:

RewriteRule ^index.php - [L]

meaning "for index.php, don't rewrite and then leave processing."