Forum Moderators: phranque

Message Too Old, No Replies

.htaccess code - redirect for images and url

         

stuckinarut

10:39 pm on Nov 10, 2008 (gmt 0)

10+ Year Member



Here is my code
RewriteCond %{REQUEST_URI} !\.(gif¦jpg¦png)$
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ myurl.com [R]

This what i am trying to do:
2 things:
1. if someone types mydomain.com/(ANYTHING THEY WANT) it will forward to myurl.com - that works--great
but...
2. if someones types mydomain/(anythingtheylike).jpg it wil forward to a certain image - this doesn't work

I know it is something really simple, i just cant figure this out.
Any help is really appreciated

Simon

jdMorgan

11:36 pm on Nov 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You've got two functions, so you need two rules. Your use of the term "forward" is ambiguous; It's not clear whether you want external redirects (change the browser address bar) or internal rewrites. The code below genertes external redirects, and works as the comments describe.

RewriteEngine on
#
# Redirect all requests for non-image URLs which do not resolve to existing files or directories to the home page
RewriteCond %{REQUEST_URI} !\.(gif¦jpg¦png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://www.example.com/ [R=301,L]
#
# Redirect all requests for image URLs which do not resolve to existing images to the replacement image
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(gif¦jpg¦png)$ http://www.example.com/certain-image.gif [NC,R=301,L]

Replace all broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

stuckinarut

4:46 am on Nov 11, 2008 (gmt 0)

10+ Year Member



big big thank you, that worked perfectly