Forum Moderators: phranque

Message Too Old, No Replies

domain name rewrite

         

sam222

8:08 am on Jul 12, 2015 (gmt 0)

10+ Year Member



Is it possible to rewrite domain name for the images where both domains exist on the same server or any other way? I don't want to copy same images to my other domain so that example1.com/images should be accessible by example2.com/images

lucy24

5:30 pm on Jul 12, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is it your own server or shared hosting? This sounds like a textbook case of the [P] flag [httpd.apache.org] in mod_rewrite.

sam222

7:39 am on Jul 14, 2015 (gmt 0)

10+ Year Member



This is my dedicated server and not shared hosting and i have root access of the server.

TheMadScientist

11:19 pm on Jul 19, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You should be able to do it within the httpd.conf vHost container for example2.com with either mod_rewrite or mod_alias.

#mod_rewrite
RewriteEngine on
RewriteRule ^/images /full/path-to/example1/images%{REQUEST_URI} [L]

OR

#mod_alias
Alias /images/ /full/path-to/example1/images/

* Something like either of the preceding should be very close to working if it's not copy/paste/edit-path ready as is.

lucy24

1:47 am on Jul 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



/full/path-to/example1/images%{REQUEST_URI}

Doesn't a leading / in the target mean domain root (as opposed to "the present directory")?

TheMadScientist

2:10 am on Jul 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I haven't played around with the mod_rewrite in that one enough to confirm, but a "similar idea/solution" from the docs implicitly indicates it should be used, by using DOCUMENT_ROOT, which is the full-root-path including the preceding /, on the right side of the following internal rewrites.

RewriteEngine on

# first try to find it in dir1/...
# ...and if found stop and be happy:
RewriteCond %{DOCUMENT_ROOT}/dir1/%{REQUEST_URI} -f
RewriteRule ^(.+) %{DOCUMENT_ROOT}/dir1/$1 [L]

# second try to find it in dir2/...
# ...and if found stop and be happy:
RewriteCond %{DOCUMENT_ROOT}/dir2/%{REQUEST_URI} -f
RewriteRule ^(.+) %{DOCUMENT_ROOT}/dir2/$1 [L]

http://httpd.apache.org/docs/2.2/rewrite/remapping.html#multipledirs

I do know the Alias works (at least on my MAMP setup) and requires the preceding /.

lucy24

6:25 am on Jul 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The original post made it sound as if the image files live in an entirely different domain-- same server, different URL-- which would change everything.

TheMadScientist

6:57 am on Jul 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



As long as the both "live" on the same server and the same httpd.conf file it will work -- You can send a request anywhere in the file system Apache has access, regardless of externally visible domain, with mod_alias and, according to the docs, with mod_rewrite, so someone could (not that someone would want to, but could) send all requests for /images/ on all sites to "below the webroot" by creating a directory in /www/ called "images" and putting: Alias /images/ /var/www/images/ in the appropriate location(s) in the httpd.conf -- I'm not sure off-the-top-of-my-head if they have to go in the vHost containers, outside of all containers, or could be either and I don't feel like "playing" any more tonight -- lol -- but it can be done in the httpd.conf.



I have my MAMP set so the server is in /Applications/MAMP, the "webroot" is in /Sites/MAMP_htdocs (on the same level as /Applications), and for testing, I sent all requests for /images/ to /Downloads/images/, which is again on the same level as /Applications and /Sites, by using:

Alias /images/ /Users/MyLocalUserName/Downloads/images/

lucy24

8:06 pm on Jul 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



creating a directory in /www/ called "images" and putting: Alias /images/ /var/www/images/ in the appropriate location(s)

Oh, OK, we are talking about something beyond vanilla rewriting. Funny how mod_alias has those two completely unrelated prongs: the alias part that can only be used in the config file, and the redirecting part that hardly anyone uses anywhere.

:: detour to check something in docs ::

Are we in 2.whatever? (I don't see any significant changes among 2.0, 2.2, 2.4, and let's not even consider the possibility that OP is in 1.3.) Docs say the Alias (or AliasMatch) directive can be used either in config or in vhost. But then, you can probably count on the fingers of one hand the directives that are permitted in config but not in vhost.

TheMadScientist

9:09 pm on Jul 20, 2015 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Are we in 2.whatever?

I'm on 2.2.16

But then, you can probably count on the fingers of one hand the directives that are permitted in config but not in vhost.

LOL -- No kidding