rewriting urls of images within a page

New to mod_rewrite? This is a good place to start.

rewriting urls of images within a page

Postby tomc » Wed Jul 08, 2009 11:37 am

First, please let me know if this is even possible. Or if there is a different method to use.

At this moment, I'm trying to figure out if there is a way to do mod_rewrite on on images links within pages.

For example

I have a webpage at http://www.example.com/sub-dir/index.php

This page contains images at http://www.example.com/_images/

Ie. http://www.example.com/_images/my-image.jpg

I created a whole new virtual server at /var/www/images/ and a soft link

ln -s /var/www/images /var/www/html/_images

(Any images referenced from /var/www/html/_images will hopefully point to http://images.example.com/

ie. http://www.example.com/_images/my-image.jpg to http://images.example.com/my-image.jpg

In my htaccess file I have so far

RewriteEngine on
#RewriteLog /var/log/httpd/mod_rewrite.log
#RewriteLogLevel 3
RewriteBase "/_images"
#RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^_images/$1 http://images.example.com/$1 [R]

Don't quite have it yet.

Is what I'm trying to do even possible? Am I using the right approach? Or is there a better approach?

Please feel free to let me know. Thanks!
tomc
 
Posts: 3
Joined: Wed Jul 08, 2009 11:14 am

Postby richardk » Wed Jul 08, 2009 2:22 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^_images(/(.*))?$ http://images.example.com/$2 [R,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby tomc » Wed Aug 05, 2009 10:57 am

richardk wrote:Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^_images(/(.*))?$ http://images.example.com/$2 [R,L]


Wanted to say thank you. That helped a great deal!

Another interesting situation...

what if you were in the same situation, but you wanted to add an SSL element to it? (if you were on a secure connection, you wanted the redirect to stay within SSL so you wouldn't have to leave and not get a popup saying "there are unsecure items on this page", etc").

You would still have the original line but I'm having trouble with these two following lines.
RewriteCond % https^_images(/(.*))?$
RewriteRule https^_images(/(.*))?$ https://images.example.com/$2 [R,L]

RewriteRule ^_images(/(.*))?$ http://images.example.com/$2 [R,L]

Would appreciate assistance. Thanks!

Tom
tomc
 
Posts: 3
Joined: Wed Jul 08, 2009 11:14 am

Postby richardk » Wed Aug 05, 2009 3:44 pm

Port number is usually reliable, though there is also the HTTPS variable. Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Port 443, https
RewriteCond %{SERVER_PORT}https ^443(https)$ [OR]
# else http
RewriteCond http ^(http)$
RewriteRule ^_images(/(.*))?$ %1://images.example.com/$2 [R,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 34 guests

cron