Forum Moderators: phranque
I recently bought a second domain and set it to point to a subfolder, but unfortunately where I have shared code and images between my subfolders/subsites by using lines such as:
img src = ../imagename.gif
these references do not work for my second domain as they are trying to load:
www.domain2.com/../imagename.gif
instead of:
www.domain1.com/imagename.gif
Do you know if there is any I can use an htaccess redirect or rewrite to correct this problem, and redirect links above the second domain root to the actual root please?
Thank you,
RewriteCond %{HTTP_HOST} domain2\.com [NC]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule (.*) [domain1.com...] [L]
The above works for loading a gif directly from [domain2...] which I was using as a test to check it was matching the problem url, but now I need to correct the redirect line.
When I use "RewriteRule (.*) [domain1.com...] the $1 is "subfolder/image.gif" prints out as so I need modify the redirect to get remove 'subfolder'.
Edit: I worked out the bug so it's working now using:
RewriteCond %{HTTP_HOST} domain2\.com [NC]
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule subfolder/(.*) [domain1.com...] [L]
I know it isn't really necessary but is there a way to speciy that the url must contain ../ as well (and without changing $1)? E.g. a version of:
RewriteCond %{REQUEST_URI} \.\.
The easiest fix is to use anchors like: <img src="/image.gif"> instead of <img src="../image.gif">
This makes the link relative to the root of your site, not relative to the current location. What current location? -- The location where the client browser thinks it is.
It is the client that resolves relative links, and these problems are easier to understand with that in mind. It then becomes obvious that your server will never see a request for "../some_resource" because the browser must resolve that to a canonical URL (http://www.example.com/some_resource) before it can send any request to your server.
HTH,
Jim