Forum Moderators: phranque
I have these rules:
RewriteRule ^boo$ /sv/boo [R]
RewriteRule ^(.*)sv/boo$ /sv/servlet?temp=portugal
I want a url that looks like:
[mysite...]
to be turned into
[mysite...] and then the second rule can do its stuff.
The two rules are working and the rewrite is happening, but the redirect is not: there is no 301 returned in the log and the url in the address bar is
just what was typed, not what was redirected to. Result is that the links in the page don't work.
Is that weird or is it what its supposed to do and I haven't understood the docs?
Any help very grateful.
An internal rewrite substitutes a different file than the requested URL would return by default, whereas an external redirect provides a new URL to be used by the client to access the resource that was originally requested. So in the simplest terms, a rewrite changes the URL-to-file realtionship, while a redirect changes URLs.
The syntax of the two forms differ, and that's probably where your problem comes in. If I understand what you are trying to do, then
RewriteRule ^boo$ http://www.example.com/sv/boo [R=301,L]
RewriteRule ^(.*)sv/boo$ /sv/servlet?temp=portugal [L]
Note that the external redirect form requires a canonical URL, including the http protocol specifier, while the internal redirect needs only a local URL-path. I have also shown the use of a 301-Moved Permanently redirect, and added the [L] flag to both rules in order to improve effciency and avoid having your internal rewrite 'exposed' to the client -- where it would be displayed in the browser address bar, for example.
Sometimes, this can go the other way, too. If your server is configured with UseCanonicalName on, then you would not need to specify the canonical URL in the redirect RewriteRule. I consider it best practice to always use the 'standard' syntax, though, so that the code is portable between hosts.
Also -- very important -- be sure to flush your browser cache before and while testing access-control, internal rewrite, and redirection code. If the browser can return a cached page by URL, it will, and the request will therefore not be sent to your server.
Jim