Apache Virtual Host Redirect: *.example.com to www.example.com - apache-config

I'm simply trying to redirect any example.com to www.example.com. The redirect currently sends example.com to www.example.com//.
The following code is in my virtual host configuration file:
RewriteCond %{HTTP_HOST} ^suksanvillas\.com$ [NC]
RewriteRule ^(.*)$ http://www.suksanvillas.com/$1 [R=301,L]
This seems to be what all the tutorials suggest, but I'm unable to understand how I'm picking up the extra "/" on the redirect. Any help on even where I might look is appreciated.
PS: Other subdomains, for examples "guides.examples.com" should not be redirected.
Thanks.

Avoid capturing the leading slash by using "^/" in the beginning of the pattern:
RewriteRule ^/(.*)$ ...
With that small change everything should work great.

Related

Trace 301 Redirect Rule and move all request to www subdomain

I have a domain example.com added to the DigitalOcean (Ubuntu 16.04) with Apache2.4.18 and have Wordpress installed on it. The site is running properly.
The problem is, that all the request www.example.com redirects to example.com. I need example.com to redirect to www.example.com.
I see there are 2 ways to do it. 1. .htaccess and 2. Apache config file, currently using 000-default.conf file. I installed letsencrypt ssl, which added the following RewriteRule to 000-default.conf
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
based on the research online, I did modify the 000-default.conf to
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
and also tried the following code in .htaccess file.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Upon doing either of these, I get error of multiple redirection loop. Which basically redirecting example.com to www.example.com and www.example.com to example.com and so on. I tested the same on Redirectcheck.com.
The main issue is I can't seem to find out the first 301 Redirect which is all the request being redirected to exmple.com. If I can disable or overwrite the main 301 Redirect rule then I can achieve all the request to be redirected to www.example.com.
I did my research but I'm hitting my head hard here, any help would be really appreciated. Thank You!
So, I finally traced it down, all the configuration were correct, the only issue was in the WordPress -> Admin Panel -> Setting -> General. I have placed the site_url and main_url as example.com and when I updated it to www.example.com and tested again, It did fix it.

Redirect all requests to HTTPS AND www to non-www

I'm fixing my Apache (2.4.12) config files on a server that serves three different domain names. I have a different config file for each site. I cannot for the life of me figure out how to accomplish both of the following:
Redirect all http requests to https, keeping the entire rest of the request (subdomain/host AND document path) exactly the same
Redirect all www requests to non-www
I've read that this can be done in one step if I have only one *:80 VirtualHost and put the rewrite rules there (the remainder of my subdomains are all *:443 VirtualHosts with the exception of www), but I can't figure out how to do it. These answers on SO did not work:
The accepted answer in this question is not correct (only does the https redirect)
This answer does not work for me--only the https redirect works.
This question doesn't deal with a wildcard subdomain and is thus inapplicable.
This question is also inapplicable because it doesn't deal with subdomains.
EDIT: This is the code I reference in the comments for mike.k's answer below.
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule ^(.*)$ https://example.com/$1 [R=permanent,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com%{REQUEST_URI}
</VirtualHost>
This is from my production system and works.
THE_HOSTNAME is for instance server, and then THE_FQHN is server.domain.edu, which helps for SSL certificates if you don't want to support wildcards and multiple domain names.
# redirect to FQHN
RewriteEngine on
RewriteCond %{HTTP_HOST} THE_HOSTNAME$
RewriteRule ^(.*)$ https://THE_FQHN/ $1 [R=permanent,L]
# redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://THE_FQHN%{REQUEST_URI}
In your case www.domain.com would be where THE_HOSTNAME is, and THE_FQHN would be domain.com, just flipped around

How do I redirect two domains to another domain with Apache?

I have three domains, two of which are supposed to be redirected to the other.
www.example.com
www.example.net
www.example.org
I already have the DNS entries setup so that they all will go to the same IP address.
What I want to have happen is for the .com and .net urls to be permanently redirected to the .org address. So:
http://www.example.com -> http://www.example.org
http://www.example.net -> http://www.example.org
http://example.com -> http://www.example.org
http://example.net -> http://www.example.org
In my .htaccess file I have the following configuration which I setup from the best of my understanding of http://httpd.apache.org/docs/2.2/rewrite/remapping.html#canonicalhost
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.org$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*)$ http://www.example.org/$1 [L,R=301]
Theoretically, what should happen is that any requests to the site where the HTTP_HOST is not www.example.org, then it should be permenantly redirected to http://www.example.org/ followed by any original path that was on the URL.
I'm sure this is easy to do and I'm just missing something obvious, but it seems like all of the other questions and search results talk about redirecting subdomains and file paths, but none of them talk about redirecting a top level domain in a URL.
Thats almost the same that I use:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.org$
RewriteRule ^/(.*)$ http://www.example.org/$1 [R=301,L]
Turns out I was on the right track. My final code wound up being this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.org$ [NC]
RewriteRule ^(.*)$ http://www.example.org/$1 [L,R=301]
The root of the problem for me was that my host was unaware I had multiple domains. So when requests would come into the site after being resolved, the host would throw up a page saying there it couldn't find a site. So I added my example.com and example.net sites to my host and parked them to example.org.
Perhaps somebody else can better explain what happened here than I, but the real issue was not with the rewrite but with my hosting provider.

redirecting with virtual hosts except robots.txt

I'm having a complicated seo issue where google has indexed thousands of pages from one of my nameservers. I need to redirect every request 301 EXCEPT robots.txt
This is what I have so far, but its not working. The commented out portion is a section that i originally put (that works), except it doesnt account for robots.txt. The two lines under that are my failure attempt
<VirtualHost xx.xx.xx.xx:80>
ServerName ns2.example.com
#RedirectMatch permanent /(.*) http://example.new/$1
RewriteCond %{REQUEST_URI} !^/robots\.txt [NC]
RewriteRule ^(.*)$ http://example.new/$1 [R=301,L]
</VirtualHost>
Does anyone see my error? example.new is the site i want to redirect to
Does anyone see my error? example.new is the site i want to redirect to
For starters, it looks like you need to turn on the rewrite engine:
RewriteEngine On
that worked, except now its redirecting everything as http://example.com//whatever-page with two //
This line:
RewriteRule ^(.*)$ http://example.new/$1 [R=301,L]
Needs to have the / removed:
RewriteRule ^(.*)$ http://example.new$1 [R=301,L]
This is because the URI that the RewriteRule is matching against has a leading slash when in vhost/server config, so you don't need the slash after the hostname in your target.

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.