Redirect htaccess without user knowledge
We are running multiple domains through the same code, and we want to store their images in their respective folders. This is what we do.
/images/www.domain1.com/logo.jpg /images/www.domain2.com/logo.jpg
now what i want to know is it possible in htaccess that we are rewriting urls without suspecting anything. This is what I want
<img src="/images/logo.jpg" />
must internally go through htaccess
RewriteRule ^images/(.*)$ /images/{HTTP_HOST}/$1 [L,R=301]
But my question is:
- The above redirecting continuously loops
- Can I get img effect if user or administrator is not suspicious?
Respectfully,
Khuram
You shouldn't use a flag R
unless you want to change the URL in the browser. However, even without the R
flag, the RewriteRule will loop endlessly and you end up with internal server error
. Use RewriteRule like this:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^images/(.*)$ images/%{HTTP_HOST}/$1 [L,NC]
A special internal variable named is used {ENV:REDIRECT_STATUS}
, which is set to 200 after the correct RewriteRule is applied.
Remove R=301
to just do a rewrite and not a redirect:
RewriteRule ^images/(.*)$ /images/{HTTP_HOST}/$1 [L]
The reason it loops constantly is because a 301 redirect causes a new request for url to be generated images/www.domain1.com/logo.jpg
. This url also matches your rule ^images/(.*)$
, so it redirects again, ad infinitum.
If you really want a 301 redirect (I suspect you didn't, but if you did), you could solve the infinite loop problem by adding some rewrite conditions to skip the redirect if the domain is already on:
RewriteCond {REQUEST_URI} !^images/www.domain1.com/(.*)$
RewriteCond {REQUEST_URI} !^images/www.domain2.com/(.*)$
RewriteRule ^images/(.*)$ /images/{HTTP_HOST}/$1 [L,R=301]