.htaccess Tip: Rewrite vs. Redirect

Category: Blog • Posted by Jeff Starr • Post Date:

Quick .htaccess tip! When using Apache mod_rewrite, you can redirect any request by including the [R] flag, for example:

RewriteRule (.*) /somewhere/ [R=301,L]

Here we are redirecting via 301 “Permanent” response. So the client will be redirected to /somewhere/.

Now here’s the trick: let’s say that, instead of redirecting, we want to rewrite the request, so that we serve some other resource at the requested URL. To do so, simply omit the [R] flag, like so:

RewriteRule (.*) /somewhere/ [L]

Here we are serving the resource /somewhere/ at the same URL that the client originally requested. So if the request was for /something/, the user will receive /somewhere/ while the browser’s address bar continues to show /something/.

This is how WordPress generates its Permalink URLs :)