Redirect only images to static domain - apache

How it's possible to 301 redirect all image (png, jpg, gif) requests from www.example.com to static.example.com?
My htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Any idea?

This should do it for you.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{REQUEST_URI} ^/(.+)\.(gif|png|jpg)$ [NC]
RewriteRule ^(.*)$ http://static.example.com/$1 [L,R=301]
Let me know how it works for you.

Related

Multiple domains on one host https and http

I have multiple domains on one host, my .htaccess looks like this
RewriteEngine On
RewriteCond %{HTTP_HOST} web1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_1/public/.*$
RewriteRule ^(.*)$ /web_1/public/$1 [L]
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_2/public/.*$
RewriteRule ^(.*)$ /web_2/public/$1 [L]
And it works so far. Now I want only web1.com, not web2.com, to redirect to https. How do I have to change the settings? (I am also thankful for tips about how to change my "implementation" so far, I am new to .htaccess and willing to learn more about it)
Try this :
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
So , your rules will look like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} web1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_1/public/.*$
RewriteRule ^(.*)$ /web_1/public/$1 [L]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} web2.com$ [NC]
RewriteCond %{REQUEST_URI} !^/web_2/public/.*$
RewriteRule ^(.*)$ /web_2/public/$1 [L]

Simple apache redirect

How can I redirect all images from www.example.com and example.com to images.example.com?
Here is some code I found online but I don't understand much and it doesn't work as expected
edit
#step 1 redirect ALL images from any domain to images.example.com
RewriteCond %{REQUEST_URI} ^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://images.example.com/$1 [L,R=301]
#step 2 redirect non www to www
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#step 3 redirect ALL non images from images.example.com to www.example.com
RewriteCond %{REQUEST_FILENAME} !\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Any help would be really appreciated!
Thanks!
Do this if you want to redirect all images to the subdomain.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://images.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^images\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/(.+)\.(gif|png|jpg|jpeg|jfif|bmp|css|js)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

.htaccess redirect subdomain before forcing HTTPS?

I have the following rules on my website:
Force HTTPS on all pages.
Redirect http://user.domain.com to https://www.domain.com/file.php?user=user
The problem I'm having now is that when someone goes to http://user.domain.com , they get redirected to https://user.domain.com , which is then insecure, instead of to https://www.domain.com/file.php?user=user .
How do I fix this?
Here is the .htaccess content:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ - [L]
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/file.php?user_id=%1 [L]
I also tried to change %{HTTP_HOST} to %{HTTPS_HOST} and it ended up with the same problem.
Try with:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^.* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(.*?)\.(www\.)?domain\.com$ [NC]
RewriteRule ^.*$ https://www.domain.com/file.php?user_id=%1 [L]
You can use these rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^ /file.php?user_id=%1 [L,QSA]

Keep referer mod_rewrite

Please help with the following question.
There is domain1.com, domain2.com, domain3.com. With the help of organized htaccess redirects to domain.com. But in the apache logs for domain.com, I do not see the referer, since when domain1 or domain2 request came.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com?$ [NC]
RewriteRule ^$ http://domain.com/$1 [R=302,L]
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^rss\.domain1\.com$ [NC]
RewriteCond %{HTTP_HOST} !^contacts\.domain2\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^rss.domain1.com$
RewriteRule ^/?$ "http\:\/\/www\.google\.com" [R=302,L]
RewriteCond %{HTTP_HOST} ^contacts.domain2.com$
RewriteRule ^/?$ "https\:\/\/www\.google\.com" [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ / [R=302,L]

force non www to www in htaccess

I have:
# BEGIN WithoutWWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
# END WithoutWWW
But it won't work. I can still access both www and non www versions. What am I doing wrong?
Try:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
//OR
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteRule ^(.*)$ [R=301,L]