How to redirect the non-www address to the www

Discuss Search Engine Optimization in relation to Joomla!. This forum will also have discussions on SEF/SEO Joomla! extensions.

Moderator: General Support Moderators

alirm
Joomla! Apprentice
Joomla! Apprentice
Posts: 15
Joined: Fri Jun 24, 2011 2:12 am

Re: How to redirect the non-www address to the www

Post by alirm » Thu Jan 05, 2012 8:32 pm

Hi guys.

I think I have a complicated problem.
here is a scenario:

I have one hosting account on Godaddy.com and two domains ( one is .com and another is .net).
I have assigned the root folder to my .com domain and it is obviously joomla.
My second domain (.net) is a simple HTML website that I have placed it on a new folder in the root(sub folder).
www.example.com = for my .com joomla website
www.example.net = Placed on a new folder on same host ( folder name is example1)

Case: I have redirected my .com from non-www to www using htaccess file and it works great.
The PROBLEM is when visitors clicking on my .net website then it redirects them to the .com and it shows the root folder not my .net domain name?!!!!( www.example.com/example1)
I have removed the redirect www and then I can see my .net domain name again.(www.example.com)
So, somehow these commands conflicted on my .net and .com domains and when I turned on redirect www then I will loose my .net URL and vice versa.

Would you please let me know how I can use www redirect for my .com and also have my example.net in the same time?

Regards
Al

alforrest
Joomla! Apprentice
Joomla! Apprentice
Posts: 8
Joined: Sun Aug 24, 2008 1:03 am

Re: How to redirect the non-www address to the www

Post by alforrest » Sat Jan 28, 2012 10:30 am

As a newbie site builder I found this thread very useful. I had been advised to use a Redirect PHP file but found that using .htaccess works much better. Also had issues because I keep the main site within a subfolder in Public_html incase I want to include subdomains. kept geting a Forbidden 403 issues until I moved .htaccess up from Mydomain folder to public_html will this likely cause problem in the future if I build other sites in this domain?

AnguyenESP
Joomla! Fledgling
Joomla! Fledgling
Posts: 1
Joined: Fri Feb 03, 2012 5:19 pm

Re: How to redirect the non-www address to the www

Post by AnguyenESP » Fri Feb 03, 2012 5:23 pm

Hey everyone,

I know this has been asked countless times but I can't seem to get the redirect to work. Here is what is in my htaccess file. Please help thank you.


##
# @version $Id: htaccess.txt 20196 2011-01-09 02:40:25Z ian $
# @package Joomla
# @copyright Copyright (C) 2005 - 2011 Open Source Matters. All rights reserved.
# @license GNU General Public License version 2 or later; see LICENSE.txt
##

##
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE!
#
# The line just below this section: 'Options +FollowSymLinks' may cause problems
# with some server configurations. It is required for use of mod_rewrite, but may already
# be set by your server administrator in a way that dissallows changing it in
# your .htaccess file. If using it causes your server to error out, comment it out (add # to
# beginning of line), reload your site in your browser and test your sef url's. If they work,
# it has been set by your server administrator and you do not need it set here.
##

## Can be commented out if causes errors, see notes above.
Options +FollowSymLinks

## Mod_rewrite in use.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^test.com [NC]
RewriteRule ^(.*)$ http://www.test.com/$1 [L,R=301]
## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site block out the operations listed below
# This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Send all blocked request to homepage with 403 Forbidden error!
RewriteRule ^(.*)$ index.php [F,L]
#
## End - Rewrite rules to block out some common exploits.

##
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##

# RewriteBase /

## Begin - Joomla! core SEF Section.
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/component/) [OR]
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#
## End - Joomla! core SEF Section.

g1smd
Joomla! Guru
Joomla! Guru
Posts: 951
Joined: Mon Feb 21, 2011 4:02 pm
Location: UK

Re: How to redirect the non-www address to the www

Post by g1smd » Sun Feb 05, 2012 10:42 pm

Your htaccess file is very old and has many problems built in.

Make sure you get a new copy from the latest version of the Joomla installer.

You should use the new copy of htaccess even if you are using an old version of Joomla.

The new file fixes a number of problems as well as showing exactly where in the file any additional redirects should go.
Online since 1995.

omdeapes
Joomla! Fledgling
Joomla! Fledgling
Posts: 4
Joined: Mon Jul 23, 2012 2:44 am

Re: How to redirect the non-www address to the www

Post by omdeapes » Tue Nov 06, 2012 1:13 pm

dhuelsmann wrote:Place it immediately after

Code: Select all

RewriteEngine On
But you may also want to check whether your rewrite is working -
Let's test to see if mod_rewrite is enabled on your server. Place only the following code in .htaccess:

Code: Select all

RewriteEngine On
Options +FollowSymLinks
Redirect /google.html http://www.google.com
Now point your browser to: http://www.yoursite.com/google.html
If it redirects you to Google - mod_rewrite is working.
If it gives you an error - mod_rewrite is not working.

Note: Replace "www.yoursite.com" with your actual web site domain.


Thank you" dhuelsmann "! This worked good for me

mpikkis
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Fri Aug 12, 2016 10:05 pm

Re: How to redirect the non-www address to the www

Post by mpikkis » Sat Oct 22, 2016 10:08 am

I have the same problem kinda.My domain is https://www.pierides-cy.com if anyone wants to see my problem in action. I have a .htaccess redirection which works ok, but the request url ALWAYS gets lost, eg. i try domain.com and it redirects to http://www.domain.com. Thats what i want. BUT if i try domain.com/blog it redirects me to http://www.domain.com and NOT http://www.domain.com/blog.I dont want subdomains redirected, and that works also
My .htaccess

Code: Select all

RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have tried this

Code: Select all

RewriteCond %{HTTPS} off
RewriteCond %{HTTPS} on
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
And it does everything i wanted to do EXCEPT redirect non-www to www

Any help appreciated !

mpikkis
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Fri Aug 12, 2016 10:05 pm

Re: How to redirect the non-www address to the www

Post by mpikkis » Sat Oct 22, 2016 10:21 am

I finally got it. You should put the code ABOVE joomla sef config, right after RewriteEngine On

tinboye
Joomla! Apprentice
Joomla! Apprentice
Posts: 21
Joined: Sat Dec 26, 2009 10:30 pm

Re: How to redirect the non-www address to the www

Post by tinboye » Fri Mar 10, 2017 11:04 pm

So I am running latest version of joomla, and trying to get redirect to work
I have SANs SSL with godaddy, so www.mysite.com is set for the https://
when I type mysite.com in the browser it goes to https://mysite.com
and then i get the error about invalid certificate.

I need it to redirect automatically to https://www.mysite.com

I have tried the above

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,QSA]

I have tried

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1

and

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* https://%1%{REQUEST_URI} [R=301,L]


none of which will work.

anyone have any further suggestions?

dannykid91
Joomla! Apprentice
Joomla! Apprentice
Posts: 11
Joined: Mon Jan 02, 2017 4:29 pm

Re: How to redirect the non-www address to the www

Post by dannykid91 » Wed Jun 07, 2017 5:24 pm

dhuelsmann wrote:Try this code in your .htaccess changing it to fit your situation

Code: Select all

########## Begin - Redirecting non-www request to www
#
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [L,R=301]
#
########## End - Redirecting non-www request to www
Thanks dhuelsmann. This is exactly what I need. Thanks so much ;) ;)

leo78
Joomla! Fledgling
Joomla! Fledgling
Posts: 3
Joined: Thu Mar 23, 2017 10:59 pm

Re: How to redirect the non-www address to the www

Post by leo78 » Sat Nov 04, 2017 5:10 pm

but it is better this way:

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]


Locked

Return to “Search Engine Optimization (Joomla! SEO) in Joomla! 1.5”