Redirect traffic to CDN page - apache

So anytime someone visits this page:
http://www.site.com/page/image_(.*).png
They get redirected to the cdn url, which would be
http://cdn.site.com/page/image_(.*).png
How can I do that using htaccess? I only need this for that url.

Using mod_alias, if you have access to vhost config, in your main vhost (for www) add:
RedirectMatch 301 ^/page/image_(.*)\.png http://cdn.site.com/page/image_$1.png
If you don't have access and have to use an .htaccess file, you need mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?site.com [NC]
RewriteRule ^page/image_(.*).png http://cdn.site.com/page/image_$1.png [R=301,L]

Related

.htaccess redirect folder to its subdomain causes redirect loop

I have a subdomain sub.domain.com
And the same contents are available on domain.com/sub
But now I want to redirect domain.com/sub to sub.domain.com
If I do that with
Redirect 301 /sub http://sub.domain.com
it causes a redirect loop.
How can this be solved technically? Users should not be able to access the subdomain content under domain.com/sub but should be redirected.
Better to use mod_rewrite rules here with conditions to restrict the rule only for domain.com requests:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteRule ^sub(/.*)?$ http://sub.domain.com/$1 [L,NC,R=302]
Make sure to test this after clearing browser cache.

htaccess redirect on a domain that already redirects

We have 3 domains in play here. Domain1.com is the main site, domain2.com is the smaller second site, and domain3.com is a vanity URL that redirects from domain1 to a Spanish version of domain2.
What we need to have happen is for domain3.com/subpage to redirect to domain2.com/subpage, however the original redirect of the vanity URL is overriding any attempts we make.
Is this kind of redirect possible?
Here is the original redirect in domain1.com's htaccess file:
RewriteCond %{HTTP:Host} ^www\.domain3\.com$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version [I,R]
These are the attempts at making the needed redirect happen (also in domain1.com's htaccess file):
RewriteCond %{HTTP:Host} ^www\.domain3\.com/subpage$
RewriteRule (.*) http\://www\.domain2\.com/spanish-version/subpage [I,R]
RewriteRule ^domain3\.com/subpage?$ /spanish-version/subpage [NC,R=302,U]
Rules for your domain3 should look like this :
RewriteCond %{HTTP:HOST} ^(www\.)?domain3\.com$ [NC]
RewriteRule ^/?(subpage_name)/?$ http://www.domain2.com/$1 [NC,R,L]
This will redirect
www.domain3.com/subpage
to
www.domain2.com/subpage
(www.)? in HTTP:HOST variable is to make the "www" part optional in url, so in this case
urls with or without "www" would also work.

redirect download directory to subdomain

my download script structure is:
http://www.mysite.com/download/viewdownload/**/**
now i want move and redirect to subdomain
for example
redirect
http://www.mysite.com/download/viewdownload/61/4249
to
http://www.alt.mysite.com/download/viewdownload/61/4249
or
http://www.mysite.com/download/viewdownload/53/824
to
http://www.alt.mysite.com/download/viewdownload/53/824
You can use either mod_alias:
Redirect /download/viewdownload http://www.alt.mysite.com/download/viewdownload
In the vhost config or htaccess in the document root of the www.mysite.com host, or use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
RewriteRule ^/?download/viewdownload/(.*)$ http://www.alt.mysite.com/download/viewdownload/$1 [L,R]

Redirecting base url only using htaccess

I'd like to redirect only the base url to an external site.
For instance, I want example.com redirected to anotherdomain.com but I don't want example.com/path to be redirected.
So far, example.com/path redirects to anotherdomain.com/path. :(
EDIT :
First, thank you for the help! example.com now redirects to another.com without affecting the children paths of example.com.
However, ideally, m.example.com won't redirect to another.com. So it's really just example.com redirecting to another.com.
Add this to your .htaccess in your DocumentRoot. I am assuming that you are hosting only one domain on the server.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^$ http://anotherdomain.com [R,L]

Need help configuring 301 permanent redirect in Apache for non www

I am trying to configure my Apache 2.2 version to use a 301 permanent redirect when someone types my url without the www. I want to configure this in the httpd.conf and not using .htaccess if possible. I have tried using Redirect permanent but the first variable has to be a directory and not a url. Any ideas how to configure boom.com requests to be redirected to www.boom.com using a 301 redirect in Apache? Thanks
Add the following:
# Canonical hostnames
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.boom\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^/(.*) http://www.boom.com/$1 [L,R=301]
This will redirect all request which don't match www.boom.com to www.boom.com, with the same query path. (For example, boom.com/foo?foo=bar will be redirect to www.boom.com/foo?foo=bar).
If you have named virtual hosts you could put the extra RewriteCond entries #tux21b gave inside to isolate them. Also if you have mod_alias you could try this which should do the same thing:
<VirtualHost boom.com:80>
RedirectMatch permanent /.* http://www.boom.com$0
</VirtualHost>
I'm sure someone will comment if there's a reason to use one over the other.