I have a very simple html page sitting at example.com. It consists solely of the following 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:
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.
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