How can I make Apache rewrite not break an image?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How can I make Apache rewrite not break an image?



I have a very simple html page sitting at example.com. It consists solely of the following index.html:


index.html


<html>
<body style="background-color:black;">
<img src="images/picture1.jpg" alt="example" style="width:40%;">
</html>



The page loads exactly as expected when I go to example.com, but I wanted to make it so any requests to example.com are redirected to this front page (so example.com/catfood takes a user to example.com). I added the following to .htaccess for this:


.htaccess


RewriteEngine On
RewriteCond %{REQUEST_URI} !=/
RewriteRule ^ / [R=301]



It redirects correctly, but it breaks the image. I believe this is happening because the request to example.com/images/picture1.jpg is now being redirected to the index page, but I'm not sure what I need to add to my .htaccess to allow the blanket redirection to happen while still loading this image on the front page.




1 Answer
1



Add another RewriteCond saying that the rule only applies if the request doesn't point to an existing file.


RewriteCond %{REQUEST_FILENAME} !-f



So:


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !=/
RewriteRule ^ / [R=301]



See
https://httpd.apache.org/docs/current/mod/mod_rewrite.html






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS