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/ /images/

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?




source to share

2 answers

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/

. 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/*)$
RewriteCond {REQUEST_URI} !^images/*)$
RewriteRule ^images/(.*)$ /images/{HTTP_HOST}/$1 [L,R=301]




All Articles