non-www to www cname redirect - apache

I'm attempting to do the following
redirect non-www to www (so example.com to www.example.com) to limit the amount of network traffic resulting from browsers sending cookies for images.
I know a 301 redirect is not advisable.
Is there a way to do this using cname dns?

If you do have access to your apache VirtualHosts configuration (http.conf) then:
<VirtualHost *:80>
ServerName example.com
Redirect permanent / http://www.example.com/
</VirtualHost>
IMHO that's the best way to do it. You can't put a CNAME record on a top-level domain so straight DNS isn't a simple solution.

If you want to redirect non-www to www then you should use a redirect. A 301 redirect is "permanent" so that is the best to use as it only needs to tell the web browser one time and the web browser then goes directly to the redirected content.

Related

Redirect non-www to www https Apache XAMPP

Please forgive what may sound like a similar question to what has been asked, however, I am VERY new to XAMPP and Apache and I have tried all possible combinations of the Rewrite Rules mentioned in other threads, which I have placed in the httpd.conf, httpd-default.conf and also in .htaccess and I simply cannot get the rewrite rules to work.
I simply want to redirect example.com to www.example.com. Please note that my redirect from HTTP to HTTPS is working 100%, so if someone can please advise on exactly where I should place the rewrite rule, and which one to use, to force non-www to www, I would be most appreciative.
I have full access to the server so I can edit either .conf or .htaccess files. Also I have checked and the part in httpd.conf that allows overrides to read the .htaccess files is enabled. The .htaccess file is currently in the same folder where my website lies. Just to add, I don't get any error, it just says the page has timed out if I type in example.com, if I type www.example.com then the page loads as expected.
Try this in your .htaccess file:
Replace example.com with your url.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
The connection has timed out
This is a DNS or server config error. The request is not even reaching your server, or your server is not responding to the request. (So any attempt to implement a redirect will naturally fail.)
A quick check of the DNS for example.com and www.example.com shows they are pointing to different IP addresses (you have an A record configured for each). The example.com host is 1 digit different and would appear to be incorrect (the IP address does not respond to requests, hence the "time out").
Instead of having a separate A record for each hostname. If the www subdomain is supposed to point to the same site as the domain apex (ie. example.com) then configure a CNAME record instead that points www.example.com to example.com (this is not a "redirect").
You then need to ensure that your server will accept requests to the non-www hostname. For example:
ServerName example.com
ServerAlias www.example.com
(It's possible you have the ServerName and ServerAlias reversed - which is "OK".)
And the SSL cert needs to cover the non-www hostname (it does).
You can then implement your non-www to www HTTP redirect as mentioned.

How to redirect www.domain.com/example to www.example.com with rewrite?

I need to redirect http://www.example.com/luis to https://www.luis.com, I have a multisite in http://www.example.com, I used the Apache mod_rewrite. For example I need redirect this:
http://www.example.com/peter to https://www.peter.com
http://www.example.com/mary to https://www.mary.com
What can I do?

301 Redirect not working correctly

I need to redirect a Blog on a subdomain running on Ghost and all its posts to a new location. The currently sits in http://blog.example.com and needs to be redirected to http://example.com/blog/
For the original Ghost blog, Apache was used as a proxy, because Ghost is running on node.js. For that reason, I cannot simply use a .htaccess in the root folder of the Ghost installation.
I used a 301 redirection generator to set all required redirects and then placed the code directly in etc/apache2/sites-enabled/000-default.conf, like so:
<VirtualHost *:80>
ServerName blog.example.com
Redirect 301 / http://example.com/blog/
Redirect 301 /post-title-1/ http://example.com/blog/post-title-1.html
Redirect 301 /post-title-2/ http://example.com/blog/post-title-2.html
Redirect 301 /post-title-3/ http://example.com/blog/post-title-3.html
</VirtualHost>
I then restarted the server.
http://blog.example.com is now correctly redirecting to http://example.com/blog/, but the individual posts are pointed to the wrong location. Instead of applying the new location, e.g. post-title-1.html, they are pointed to http://example.com/blog/post-title-1/, which logically throws a 404 error.
Would appreciate your advive, how to solve this.
You have rules in wrong order i.e most generic catch-all rule is your first rule and taking precedence over rest of the rules.
Use:
<VirtualHost *:80>
ServerName blog.example.com
Redirect 301 /post-title-1/ http://example.com/blog/post-title-1.html
Redirect 301 /post-title-2/ http://example.com/blog/post-title-2.html
Redirect 301 /post-title-3/ http://example.com/blog/post-title-3.html
Redirect 301 / http://example.com/blog/
</VirtualHost>
Make sure to clear your browser cache before testing this.

Remap domain.io to show as domain.com in address bar

I have domain.io and domain.com BOTH pointing to the SAME server. How do I set this up so that domain.io never shows up in the address bar, and it's always domain.com?
Thanks!
You can use Apache's mod_alias Redirect directive for this:
<VirtualHost *:80>
ServerName domain.io
ServerAlias www.domain.io
Redirect permanent / http://domain.com/
</VirtualHost>
In your case, permanent (301) redirect sounds appropriate but you may want to look at HTTP redirect: 301 (permanent) vs. 302 (temporary)
Also, it's really unimportant where the domains are hosted in this case; they do not need to be on the same server. In fact, many DNS and web host providers actually provide this functionality as a service, and will host the equivalent of above configuration for you (though not necessarily with Apache; nginx is probably more likely for this purpose these days).

Apache Redirects in HTTPS

I have an Apache server (https://mysite.com/sub/en/). It's setup with SSL on port 443, and in the VirtualHost tags:
<VirtualHost _default_:443>
Redirect permanent /sub https://mysite.com/sub/en/
...
</VirtualHost>
Basically, when the user comes to the site, I want them to be forced to the "/en" sub-directory. However, if you try to visit https://mysite.com/sub, it doesn't redirect you to the sub "/en" directory.
I'm really not sure what is going on. I do have a similar setup in my non-ssl virtualhost tags:
<VirtualHost *:80>
Redirect permanent / https://mysite.com/sub/en/
Redirect permanent /sub https://mysite.com/sub/en
</VirtualHost>
These rules work fine. So if the user attempts http://mysite.com/sub, it forwards them to the SSL version, and the "/en" subdirectory like I want it to. But if you try the SSL version: https://mysite.com/sub, it fails to forward.
Do I have the configurations wrong? Am I not using the right redirect or rewrite rule?
To get this working the way I wanted, I ended up using a RewriteRule:
RewriteRule ^/sub$ https://mysite.com/sub/en/
This appears to have done the trick.