Apache Rewrite: HTTP host-based image catalog

My software supports several domain names, all listed in the same directory on the server (for each of them, of course, a different database). So these domains ...

www.example1.com www.example2.com www.example3.com 

... everyone points to ...

 /public_html/ 

In the image catalog ...

 /public_html/images/ 

I have directories that exactly match the host names for each website ...

 /public_html/images/www.example1.com/ /public_html/images/www.example2.com/ /public_html/images/www.example3.com/ 

I'm trying to get Apache to rewrite requests so that if you view the image directly and look at the address bar, you will only see the host name once.

So, the request for ...

 http://www.example1.com/images/book.png 

... Apache is retrieved at ...

 /public_html/images/www.example1.com/book.png 

One of the things that I tried and was successful in different circumstances is as follows, although this does not work in this situation ...

 RewriteRule ^[^/]*/images(.+) images/%{HTTP_HOST}/$1 

We will be glad to make any clarifications, as well as voting, and accept the answer, if someone can help me get this work to work.

+4
source share
3 answers

Try adding the following to your .htaccess file in the root directory of your site (public_html)

 RewriteEngine on RewriteBase / #prevent looping from internal redirects RewriteCond %{ENV:REDIRECT_STATUS} !200 #only rewrite gif, jpg or png RewriteRule ^(images)(/.+\.(gif|jpg|png))$ $1/%{HTTP_HOST}$2 [L,NC] 

Your rule

 RewriteRule ^[^/]*/images(.+) images/%{HTTP_HOST}/$1 

doesn't work because you have a lead / up images . In .htaccess, the leading / is removed, so the rule will never match.

+1
source

Here is one of the things I have done for my high performance (see my biography).

I give you an advanced RewriteRule, I am sure that you will have enough material to complete:

Creating static domains:

 static.example1.com static.example2.com static.example3.com 

Where all your images will be.

From now on, no more than:

 www.example1.com/images/www.example1.com/picture.jpg www.example2.com/images/www.example2.com/picture.jpg www.example3.com/images/www.example3.com/picture.jpg 

but

 static.example1.com/picture.jpg static.example2.com/picture.jpg static.example3.com/picture.jpg 

Good urls? Now create vhost with all your static files:

 <VirtualHost *> ServerName static.example1.com ServerAlias static.example2.com static.example3.com </VirtualHost> 

Set the root of the document to the database without the name vhost, so in your case:

 DocumentRoot "/public_html/images" 

And add this RewriteRule

 RewriteCond %{HTTP_HOST} ^static\.([a-zA-Z0-9\-]+)\.com$ # Change the path, and add the request: RewriteRule (.*) %{DOCUMENT_ROOT}/static.%1.com$1 [QSA,L] 

So, all:

 <VirtualHost *> ServerName static.example1.com ServerAlias static.example2.com static.example3.com RewriteCond %{HTTP_HOST} ^static\.([a-zA-Z0-9\-]+)\.com$ # Change the path, and add the request: RewriteRule (.*) %{DOCUMENT_ROOT}/static.%1.com$1 [QSA,L] </VirtualHost> 

It’s good that it doesn’t answer exactly your question, so here is a short answer, but I don’t like it because it won’t help you do a very (very) good job:

  RewriteCond %{HTTP_HOST} ^www\.(example1|example2|example3)\.com$ # Change the path: RewriteRule (.*)(\.(css|js|txt|htc|pdf|jpg|jpeg|gif|png|ico))$ %{DOCUMENT_ROOT}/www.%1.com$1$2 [QSA,L] 

And if this is not enough:

Two tips:

If you are hosting in a non- hosted environment (= if your own server can change virtual hosts, and not just .htaccess ), try using the RewriteLog directive: this will help you identify such problems:

 # Trace: # (!) file gets big quickly, remove in prod environments: RewriteLog "/web/logs/mywebsite.rewrite.log" RewriteLogLevel 9 RewriteEngine On 

My favorite regexp checker tool:

http://www.quanetic.com/Regex (don't forget to select ereg (POSIX) instead of preg (PCRE)!)

+1
source

John

I just posted a separate Q for some of the problems you are facing. I would welcome your comments, but back to your problem: one trick in which you can use an environment variable to store your (preferably verified) host, for example:

 RewriteCond %{HTTP_HOST} ^www\.(host1|host2|host3\.com RewriteRule ^ - [E=HOST:%1] 

You can also add [S] flags to implement if / then / else logic in your rules. And you can also use the HOST variable in the following rules or condition lines (and not in regex patterns) as% {ENV: HOST}.

You also need to clearly view the full phpinfo () report to see if the hosting service uses mod_php or mod_suPHP ... and the host supports DNS multithreading. For example, my provider sets% {ENV: DOCUMENT_ROOT_REAL}, which I need to use instead of% {DOCUMENT_ROOT} when viewing the file space.

All your “arrival” URIs in DOCROOT / are of the form http://www.exampleX.com/images/book.png , so if your .htaccess location is your DOCROOT, then your base is / . Thus, provided that ENV is installed, they should work

 RewriteBase / RewriteCond %{HTTP_HOST} ^www\.(host1|host2|host3)\.com RewriteRule ^ - [E=HOST:%1] RewriteCond %{ENV:HOST}==%{REQUEST_URI} !^(.*?)==/image/\1 RewriteRule ^image/(.*) image/%{ENV:HOST}/$1 [L,NS] 

Cond is an interrupt to stop the rewriting cycle of rules.

Generalized version

The above solution is already a generalized solution like you. Just replace the RewriteCond regular expression with any pattern matching your own naming convention, and I agree that if it is (. *), You can also remove the first rule and replace %{ENV:HOST} with %{HTTP_HOST}. . You need a RewriteCond protector to prevent a loop that results in 500.

+1
source

Source: https://habr.com/ru/post/1394891/


All Articles