Q. What’s the best way to redirect domain.com to www.domain.com on the url address bar whenever someone omits or skip the www from domain.com? My site can be reached by two different URLs. For example http://domain.com/about/us.html and http://www.domain.com/about/us.html
How do I do configure Apache web sever vps for this task?
A. Apache (web server) 301 redirect is the most efficient and search engine friendly method for webpage redirection. You can place following code Apache’s httpd.conf vhost section or in .htaccess file.
$ vi .htaccess
Append following config code:
RewriteEngine on RewriteCond %{HTTP_HOST} ^domain\.com RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
Save and close the file. Above code will redirect users to www.domain.com url.
🐧 30 comments so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
Thanks for the info and great site!
There actually is a little correction to be made.
With the code you wrote, if you enter the site
http://domain.com/index.php
you are redirected to
http://www.domain.com//index.php
Notice the double slashes.
The correction you need to do is on this line:
RewriteRule ^(.*)$ http://www.domain.com$1 [R=permanent,L]
just remove the slash before $1
no no. Its working fine. The code is alright…
Good point – remove that slash.
If you have mod_alias enabled, then no need for Rewrites.
Use this to redirect requests to domain.com to http://www.domain.com
RedirectPermanent / http://www.domain.com
This will Redirect http://domain.com/anything/foo/bar/baz to http://www.domain.com/anything/foo/bar/baz
mod_alias is available in Apache 1.3 also.
Use mod_alias’s RedirectPermanent directive.
RedirectPermanent / “http://www.domain.com/”
it will redirect
domain.com/foo/bar/baz to http://www.domain.com/foo/bar/baz
mod_alias is available in Apache 1.3 also
Nice tip, even nicer comments!
You should really setup another vhost that has servername domain.com and which redirects to http://www.domain.com/ and then another vhost for http://www.domain.com else you get stuck in infinite redirect loop.
ServerName domain.com
Redirect permanent / http://www.domain.com
ServerName http://www.domain.com
if i do this but i have seen DNS 501 error
heh, forgot it would strip vhost tags… :(
<VirtualHost *:80>
ServerName domain.com
Redirect permanent / http://www.domain.com
</VirtualHost>
<VirtualHost *:80>
ServerName http://www.domain.com
</VirtualHost>
All this code looks nice, but it doesn’t help me any. If I knew this code and knew exactly where it goes, I wouldn’t be here. If you guys could explain this in detail it would help people like me. Like some installation instructions. To me, all you guys are doing is having a conversation amongst yourselves. Thank you
john, don’t be so helpless – this site doesn’t exist to serve you
either read up a little bit or pay someone to solve your exact problem
you should have a file called httpd-vhosts.conf somewhere in your setup (inside the apache/conf/[something] directory most likely) that’ll have “” in it somewhere. It goes between the and tags. Add a second entry if you also use port 443 (https).
If you are hosting multiple domains in a server, you might just add:
ServerAlias domain.com
in the .conf file of your domain. That’s it!
The example provided will redirect visitors which are trying to reach domain.com to http://www.domain.com. But however, it won’t redirect other subdomains, for instance mistyped URL:S like wwww.domain.com or mail.domain.com.
This example will redirect everything which is not http://www.domain.com to http://www.domain.com (provided that subdomains are pointing to the same place):
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
And for those of you which want the opposite, i.e. that whatever the visitor writes he gets redirected to domain.com not using any subdomain, use this code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^.*\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
I don’t understand the problem why, but the Redirect permanent solution is recommended for http to https around the net, too. However, it causes an infinite redirection loop, which is not what is needed! Rewrite rules work.
If you’re using friendly urls, caught by a 404 handler like me, you can use the following code to append the friendly url to the new domain:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com%{REQUEST_URI} [R=301,L]
I think what you really need is just a alias
server name domain.com
server alias http://www.domain.com
my particular opinion.
This does not seem to be a good solution for wordpress sites that have already been configured where the full URL has already been stored in the database. This solution will cause the app to report database connection errors. I imagine that this would also affect other similar apps with mysql DB backend.
Disclaimer, I’m not an Apache expert, and know little about mod_rewrite
I think the best option is to re-write the URL when the client browser requests http://domain.com, and instruct the browser to request http://www.domain.com
It seems like this is the best way to not break an app that connects to the database.
this site is very good !!
Hi!
I have an Apache web server with >100 domains, with virtualdomainalias. I need a rewrite rule with regexp to reach the following (N,M=1-100):
domain-N.tld -> http://www.domain-N.tld
subdomain-M.domain-N.tld -> subdomain-M.domain-N.tld (and not to http://www.site.domain-N.tld)
Thanx!
Andreas
This has few great examples: http://httpd.apache.org/docs/1.3/misc/rewriteguide.html
Could you please add how can we do the for co.in and .co.cc like domains which will be more helpfull
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.co\.in$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.in$1 [L,R,QSA]
To rewrite for multiple domains in one rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST
Hope there is some mistake this will not work. please help to correct this !!
Here is a more general rule that will map any *.com URL to www.*.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^.*\..*\.com$ [NC]
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}\.com$1 [L,R=301]
Corrected:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^.*\..*\.com$ [NC]
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}$1 [L,R=301]
In http it works…. but
I have tried the above for https but its not work.
ewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ https://www.domain.com/$1 [R=permanent,L]
What is now the correct term to redirect websites from http:// to http://www?
is it the following one?
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
We have a website hosted by Vodafone.
The website is reachable under http://www.example.de but not http://www.example.de.
I mailed the service team of Vodafone but got no really satisfying answer.
How can we do that with htaccess-file?
How would I redirect the domain but keep the URL ..
example I want
if someone goes to http://domain.com
I want it to go to http://domain.com/test/index.html
But want the URL to say domain.com
Add a “ServerAlias example.com” under the “ServerName http://www.example.com“