Search code examples
.htaccessmod-rewriteurl-rewritingsubdomainmod-proxy

If mod_proxy is unavailable, what is the effect of not using the P flag in an htaccess rewrite?


For a mobile subdomain (m.domain) I'm simply trying to pull images from the common main domain image folder (www.domain.com/images)

After a bit of tinkering, I've apparently gotten the htaccess rewrite code to work successfully but I found out I was unable to use the P flag on rewrite, and that I don't have mod_proxy enabled due to shared hosting.

simple code in my m.domain.com htaccess file:

RewriteRule ^(images/.*) http://www.domain.com/$1 [L]

What benefit am I not receiving by not using the P flag? What's the difference?


Solution

  • When you proxy a request, the server itself does the external request and passes through the data it receives. If the client would request http://m.example.com/asdf, and the server would proxy to http://www.example.com/asdf, the server would make a proxy-request to that url, then return that data for http://m.example.com/asdf to the client. For the outside world there is duplicate content on http://m.example.com/asdf and http://www.example.com/asdf, and any client requesting both urls will cache things twice. This is not ideal.

    With your current approach you make an implicit temporary redirect. When the client requests http://m.example.com/asdf, the server sends back a redirect-response, which tells the client to try again on http://www.example.com/asdf. The client tries again on that url, and receives the data from the server. For the outside world it is clear that the content is in one place, and when caching the data it will only cache it for http://www.example.com/asdf. If you make the redirect permanent, it will skip the redirect in some cases and directly request http://www.example.com/asdf, reducing the load on your server.

    Unless you have a good reason to proxy a request (e.g. because the server with the data is only available locally), you should not use a proxy request. If http://m.example.com/ and http://www.example.com are hosted on the same server, and you are unconcerned about hosting duplicate content, then you should use an internal rewrite. If you are concerned about duplicate content, you use an external redirect, using the [R] flag.