Need help configuring 301 permanent redirect in Apache for non www - apache

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.

Related

OpenShift and redirect in .htaccess

I have a Wordpress app deployed on OpenShift and a domain alias associated, i.e. www.example.org. Now I would like to add another alias, i.e. www.example2.org, and gracefully redirect all the request from www.example.org => www.example2.org.
I tried to do this via .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.org$
RewriteRule (.*)$ http://www.example2.org/$1 [R=301,L]
Whenever contact www.example.org it generates an infinite loop of redirects and I can not understand why.
Openshift rewrites the redirect header, but you can prevent this by adding the port number to the URL. See more here.
There's no need for RewriteCond. And example.org is not an alias of example2.org. It's a brand new A record. You can eventually redirect www.example.org via your domain registrar's dashboard or create index.php with header('Location: http://www.example2.org');.
RewriteEngine on
RewriteRule ^(.*)$ http://www.example2.org/$1 [R,QSA,L]

Redirect all request from old domain to new domain

I am looking to migrate from old domain to new domain.
I have my old domain olddomain.com and new domain newdomain.com pointing to same ip address for now.
I have Apache server inplace to handle requests.
How do I 301 redirect all my
olddomain.com/*
&
www.olddomain.com/*
to
newdomain.com/*
Can I get exact regex or configuration that I need to add in htaccess.
My newdomain.com and olddomain.com both are being serverd by same apache from same IP so "/" redirect might lead to cycles? And so was looking for effecient way
I tried
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$ [OR]
# RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://comp16/$1 [R=301,L]
</IfModule>
And even tried adding in virtual host
RedirectMatch (.*)\.jpg$ http://comp17$1.jpg
But it does not redirect site when i hit localhost in browser to my computer name i.e comp16
In the configuration (VirtualHost) for each of your olddomain.com host try this:
Redirect permanent / http://newdomain.com/
Apache documentation for Redirect. This is the preferred way when everything should be redirected. If you must use mode_rewrite/htaccess there are plenty of questions around this on SO and one of them is:
How do I 301 redirect one domain to the other if the first has a folder path
EDIT
Recommendation from Apache regarding simple redirects:
mod_alias provides the Redirect and RedirectMatch directives, which provide a means to
redirect one URL to another. This kind of simple redirection of one URL, or a class of
URLs, to somewhere else, should be accomplished using these directives rather than
RewriteRule. RedirectMatch allows you to include a regular expression in your
redirection criteria, providing many of the benefits of using RewriteRule.
I also recommend to use an If statement as you can use it also in a multisite server. Just type:
<If "%{HTTP_HOST} == 'old.example.com'">
Redirect "/" "https://new.example.com/"
</If>
Write the below code in to your .htaccess and it will redirect all your old domain request to new domain.
RewriteEngine on
RewriteBase /
RewriteRule (.*) http://newdomain.com/$1 [R=301,L]

Redirect traffic to CDN page

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]

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]

Redirect with htaccess for images onto another server without redirect looping

I currently have a host where my main site is hosted on. I have set up nginx on another server to mirror/cache files being requested if it doesn't have it already, in particular images and flv videos.
For example:
www.domain.com is my main site.
www.domain.com/video/video.flv
www.domain.com/images/1.png
I would like to ask apache to redirect it to imgserv.domain.com (imgserv.domain.com points to another server IP)
imgserv.domain.com/video/video.flv
imgserv.domain.com/images/1.png
Basically redirect everything with certain filetypes and preserving the structure of the URL, like flv etc.
I tried something but I am getting a redirect looping error. Could someone help me out?
Thank you!
This is what I have at the moment
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RedirectMatch 302 ^(.*)\.gif$ http://imgserv.domain.com/forums$1.gif
RedirectMatch 302 ^(.*)\.jpg$ http://imgserv.domain.com/forums$1.jpg
RedirectMatch 302 ^(.*)\.png$ http://imgserv.domain.com/forums$1.png
You are mixing up two different modules: RewriteEngine and RewriteCond are from mod_rewrite while RedirectMatch is from mod_alias. They can’t work together.
Try this mod_rewrite example instead:
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^.*\.(gif|jpg|png)$ http://imgserv.example.com/forums/$0 [L,R]