Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess redirect but exclude images

I'm trying to use htaccess redirect to a file, but can't get the image to display.

I want to redirect ANY request to mydomain.com or www.mydomain.com to www.mydomain.com/test.html

This is the content of my htaccess file:

# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !=/test.html
RewriteCond %{REQUEST_URI} !=*\.png$ [NC]
RewriteRule .* /test.html

I have this line of code in my test.html file:

<img src="image.png" alt="My Image">

But the image doesn't display at all, I just get a broken image. I also tried using absolute path for image, same thing.

like image 881
user1078494 Avatar asked Nov 29 '22 17:11

user1078494


2 Answers

Try replacing your .htaccess file with the following

# Turn rewriting on
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

#if the request does not end with test.html or is not a .png
RewriteCond %{REQUEST_URI} !(test\.html|\.png)$ [NC]
# Rewrite it to test.html
RewriteRule .* /test.html  [L]
like image 62
Ulrich Palha Avatar answered Dec 04 '22 09:12

Ulrich Palha


This will exclude various types of images

#if the request does not end with .gif,.png or jpg 
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
RewriteRule ^index.html$ index.php [NC] 
like image 40
Rinto George Avatar answered Dec 04 '22 10:12

Rinto George