简体   繁体   中英

.htaccess rewrite for multiple domains

I am centralizing all of my images into one folder on my server. I have several websites that use a single centralized engine to get their respective contents for each domain. The directory structure for the images is:

images/
  website1.com/
    image1.jpg
    image2.jpg
    ...
  website2.com/
    image1.jpg
    image2.jpg
    ...
  website3.com/
    image1.jpg
    image2.jpg
    ...

However, when I request one of these image resources from the HTML, I do not want to use the URL:

http://website1.com/images/website1.com/image1.jpg

Instead I simply want:

http://website1.com/images/image1.jpg

There are 2 scenarios where I need this translated however. Live server and development server. The live case seems easy, rewrite the %{HTTP_HOST} into the path. The development server case is a little different. I use a cookie called dev_domain to be able to view the test sites. ie: $_COOKIE['dev_domain'] = website1.com.

What are the rules to make both the live case and development server case?

This is what I have so far for the development case, but it doesn't work:

RewriteBase /
RewriteCond %{HTTP_COOKIE} dev_domain=([^;]+) [NC]
RewriteCond %{THE_REQUEST} ^/images/(.*)$
RewriteRule ^/images/(.*)$ images/%1/$1 [NC,L]

This is what I have for the live case, not sure yet if it works or not because I have not pushed it live (the condition is to exclude the development server):

RewriteCond %{HTTP_HOST} !^dev.mainsite.com$
RewriteRule ^/images/(.*)$ /images/%{HTTP_HOST}/$1 [NC,L]

Any ideas on what I'm doing wrong?

You look to be nearly there. The RewriteRule directive should not have a leading /

# Added [NC] to the host RewriteCond, removed [NC] from the RewriteRule
RewriteCond %{HTTP_HOST} !^dev.mainsite.com$ [NC]
RewriteRule ^images/(.*)$ /images/%{HTTP_HOST}/$1 [L]
#----------^^^---no leading /

Apply the same change pattern to your development site rules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM