1

I have this domain sub.example.com. And I want to redirect to an image in a subfolder without changing the url. Like this:

sub.example.com/34   ==>  sub.example.com/images/34.png   # sub.example.com/34 is url but an image is on the screen

I tried this:

RewriteCond %{REQUEST_URI} (.*)
RewriteRule ^(.*)$ https://sub.example.com/images/$1.png

But this just becomes an infinity loop, because it's redirecting again and again. And it also doesn't use $1 (number behind whole domain).

Thank you in advanced :)

Jakob
  • 1,858
  • 2
  • 15
  • 26
  • If you don’t want to change the URL shown in the browser, then `R=301` is of course wrong to begin with. You want an internal rewrite then, not a redirect. – CBroe Apr 06 '20 at 10:11

1 Answers1

1

You may use this rule in site root .htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ images/$1.png [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks for your answer. When I add it to the directory where my image folder is, I get an infinity site request loop again. What do you mean with "site root"? Now it's just preventing redirecting in loop. – Jakob Apr 05 '20 at 08:52
  • This rule should be placed in root .htaccess not in images folder. Also you need to test if **in a new browser** or after clearing browser cache only. – anubhava Apr 05 '20 at 10:50
  • There is no loop anymore, but now just a white page is show and not the image :( – Jakob Apr 05 '20 at 11:05
  • Do you have other rules in this .htaccess by any chance. Rules don't cause any noticeable slowness. Test in Chrome dev tool with **caching disabled** and check in Network tab what are 301/302 redirect URLs you get. – anubhava Apr 05 '20 at 11:08