[RESOLVED] Rewriting content on the fly
Hello there,
I have what may look as a strange request.
I have to change the content of a webpage hosted on Apache, without actually changing the file. The webpage can be a .php, .html or .htm.
What I want to do is to rewrite the url of the images it contains, so they point to another location. For instance, imagine this is part of the original content:
<img src="images/logo.jpg" alt="Company logo" />
I have to rewrite the src value, and make it look like this:
<img src="http://whatever.anotherdomain.com/images/logo.jpg" alt="Company logo" />
For a reason I won't explain (long story), I can't just open the page and modify the src attribute manually. It has to be done by a script, either a Javascript or other method.
Even if I'm able to accomplish this via Javascript, I'm looking for another solution because if the user disables javascript, the src value is not rewritten.
My question:
Can I do this with an .htaccess rule? How?
If not, are there other options?
Many Thanks in advance. :wave:
edit: I suddenly remembered that I can add the <base> tag to the page instead of rewriting all the SRCs, and get the same result. The question is still there: how do I add that tag on the fly?
Re: Rewriting content on the fly
Check out the file functions. You can make a script that can go through your pages and add that, but it might be difficult.
Re: Rewriting content on the fly
Thanks for your reply.
In the meantime I realized the proper use for the .htaccess file. I didn't have to necessarly rewrite the contents of my pages, but just redirecting the requests was enough.
I solved my problem by putting the following inside the .htaccessfile
Code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.myotherdomain.com$ [NC]
RewriteRule ^(.*)\.(bmp|tif|gif|jpg|jpeg|jpe|png)$ http://www.myotherdomain.com/$1\.$2 [L,R=301]
ie. Apache redirects all the incoming requests to image files to the other domain.
Thanks for helping anyways.