• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 514
  • Last Modified:

mod_rewrite on Apache 1.3 and image paths

I've installed mod_rewrite according to http://www.experts-exchange.com/Web/Web_Servers/Apache/Q_21180364.html?query=mod_rewrite but although I think the rewrite is working for one site on my local server it has caused other sites to lose their images paths references. I'm using osCommerce and if I return to the previous httpd.conf then the image buttons display correctly.

The htaccess files in the directories where the image path refs are lost do not include any reference to mod_rewrite, only the htaccess file in the one dir has RewriteEngine On.  

I've seen some threads about mod_rewrite and image paths:

However if I set 'RewriteEngine On' in an htaccess file within a sub-directory of my webserver I'm not expecting it to effect the image paths in other sub-directories of the web server.
  • 6
  • 3
1 Solution
It will affect paths, if you use relative links, because *the browser* requests the wrong path.

I.e you're using a virtual folder, /abc/. If you use <img src="12.jpg">, the browser would request /abc/12.jpg instead of /12.jpg. This has nothing to do with your .htaccess, but only with your virtual folder structure.

So, if you're using relative imagepaths in your html source code and "virtual folders", change them to absolute image paths by adding a leading slash <img src="/12.jpg"> or use <base bref="http://example.com/> in the <head>-Section

> However if I set 'RewriteEngine On' in an htaccess file within a sub-directory of
> my webserver I'm not expecting it to effect the image paths in other sub-directories
> of the web server.

The rules will affect subdirs of that subdir, if the pattern (left part of the rewriterule) matches.
ncwAuthor Commented:
I have the following simple folder layout and I've checked that there is no htaccess in wwwroot (maybe there should be?):

     |_ dir1 with rewrite
     |               |_ other sub-dirs
     |               |_ .htaccess with rewrite
     |_ dir2 without rewrite but still affected
                      |_ other sub-dirs
                      |_ .htaccess without rewrite
The site images in dir2 worked fine with relative paths until I installed the mod_rewrite in httpd.conf.

So it looks like I'm going to have to put <base bref="http://example.com/> in the <head>-Section in every page if I want to use mod_rewrite and don't want to change all image paths.
ncwAuthor Commented:
Also, external hosts have mod_rewrite installed and I don't have any issues with the same relative image paths.
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

ncwAuthor Commented:
The point in my last comment is that installing mod_rewrite in Apache's httpd.conf shouldn't upset calls to relative paths if the rewrite engine is not switched on in an htacess file; but it appears to be in my case.
This are two different things:
- relative paths and virtual folders on one hand and
- rewriteRules rewriting imagepaths on the other hand.

Relative paths and RewriteRules have nothing in common.

Either you use a virtual folder structure and relative paths, which ends up in wrong requests  /abc/12.jpg instead of /12.jpg (take a look on your access.log) OR your RewriteRules are rewriting requests of images to an other location (conflict with rules).
If you use RewriteRules such as
RewriteRule !^123 - [C]
RewriteRule ^(.*) /123/$1 [L]
all images i.e. request by the browser was /12.jpg would be internally rewritten and called from /123/12.jpg

RewriteRules in httpd.conf are applying to all requests, where the rules are seen. If you use the rules in a virtualhost for server1.com, they are only seen by requests for server1.com but not for server2.com.

Again, it depends upon your rule and condition pattern's, if rewriteRules are rewriting your imagepaths. You can take a look at the RewriteLog to find out where your rules and conditions did apply and to what path they've rewritten the initial request.
ncwAuthor Commented:
Something in the way I have installed mod_rewrite has affected the processing of relative image paths for websites in sub-folders of wwwroot, where .htaccess contains no reference to rewrite. So it's like the rewrite engine is on by default.

It maybe that my initial installation of virtual folders is not correct? To access the sites I use eg<foldername>/
(and I have to add the last forward slash else it won't find the index.php).
It can never affect the processing of relative image paths because mod_rewrite is not a markup praser, it doesn't change your HTML-Source.

It can only rewrite requests to an other location, but this has nothing to do with relative or absolute paths but with the browser's request. Please check your access log and rewrite log.
ncwAuthor Commented:
I've not taken this any further because I'm a bit confused about the situation. I've had a look in the access.log and all paths begin with '/', there doesn't seem to be a rewrite.log.

Can we step back a stage please and discuss whether I've setup Apache correctly in terms of 'virtual folders'? If I switch config files between the one with and the one without rewrite mod then the image paths fail with rewrite mod installed but work ok without rewrite installed. The only htacces file that uses rewrite is in one of the sub-folders.

The httpd.conf file in place at the moment does not have rewrite installed.
ncwAuthor Commented:
My problem is that I still haven't been able to setup virtual folders correctly.

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now