0

So I recently sorted all my files on a subdomain into different folders, let's just call it upload.example.org. One of the new folders is titled "img" (public_html/upload/img), where I now automatically upload any pictures I take for easy sharing. Previously all images were in the document root folder "upload" and that's where I used to share the URLs from, so a redirect to the new URL is needed to avoid breaking anything.

Now what I've been trying to do is create a .htaccess that redirects people from upload.example.org/image.jpg to upload.example.org/img/image.jpg. image.jpg can be replaced with any other file name in the img folder.

Looking for answers, I found this:

RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

Using this line of code with my subdomain results in a loop, in the end redirecting me to http://upload.example.org/img/img/img/img/img/img/img/img/img/img/image.jpg.

After searching and trying out different .htaccess variants, I couldn't come across the correct solution.

Any help that you can give would be much appreciated! :)

Mikusch
  • 125
  • 1
  • 8

1 Answers1

0

The reason for the loop is because after the redirect, the client requests the new URL and this new URL is redirected again, causing a new request from the client and again a redirect, and so on ...

To avoid this, you must prefix the rule with a stop condition, e.g.

RewriteRule ^img/ - [L]

This causes an exit to the rewrite chain, if the requested URL already starts with img/.


Another approach would be to test first if an appropriate file exists in the img folder, and only redirect if there is one.

RewriteCond /img/$1 -f
RewriteRule ^(.*)$ ^img/$1 [R,L]

If you want to exclude a folder from any rewrite, use a stop condition rule as in the above example

RewriteRule ^path/to/some/folder - [L]

This ensures, that any request beginning with the given URL path is ever rewritten or redirected.

If you want an exception from only the img rule, you could add another condition to the rule, which checks if the resulting path would be in this other folder, e.g.

RewriteCond /img/$1 -f
RewriteCond /img/excluded/folder/$1 !-f
RewriteRule ^(.*)$ ^img/$1 [R,L]

These conditions check first, if the image exists somewhere in the img subfolder, and then make sure it is not a file in the img/excluded/folder.


Unrelated, but never test with R=301! When everything works as it should, you may replace R with R=301.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Thank you, very helpful. One last request, could you show me how the full .htaccess has to look like then? – Mikusch Jan 28 '17 at 13:52
  • You can either put the first `RewriteRule` in front of your other rules. Or if you want to try the second rule instead, replace the existing `RewriteRule` with the `RewriteCond/RewriteRule` pair. – Olaf Dietsche Jan 28 '17 at 13:55
  • These rules only work, if the .htaccess is in the `upload` folder. When the .htaccess is in the root directory, you must replace `img` with `upload/img` of course. – Olaf Dietsche Jan 28 '17 at 14:00