Forum Moderators: phranque

Message Too Old, No Replies

Htaccess redirect above domain

Redirecing url from above root

         

DonX

12:56 pm on Mar 19, 2005 (gmt 0)

10+ Year Member



My htacess file is in my filespace root directory, and I have a main domain url the points to this.

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,

DonX

2:34 pm on Mar 19, 2005 (gmt 0)

10+ Year Member



Actually I should probably include what I have written so far.

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} \.\.

jdMorgan

6:36 pm on Mar 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



DonX,

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

DonX

7:07 pm on Mar 19, 2005 (gmt 0)

10+ Year Member



Ah, that is very useful indeed. I would have never guessed that would use the filespace root and not the domain root. Fantastic. Thank you very much for your reply :)