Forum Moderators: phranque

Message Too Old, No Replies

301 redirect from non-www to www for subdirectory

301 redirect from non-www to www for subdirectory

         

cheyingtan

4:09 am on Jul 19, 2010 (gmt 0)

10+ Year Member


Hello,

I was spending 3 hours searching in Google and doesn't find anything that can solve my problem. Can anyone help? Thanks in advanced.

I am trying to setting up the 301 redirect for my Joomla site.
It is installed in the subdirectory as below:
http://www.mydomain.com/joomla

When somebody type in http://mydomain.com/joomla, i want it to be http://www.mydomain.com/joomla (From non-www to www to avoid canonical issue)

But I couldn't get .htaccess code to work properly in subdirectory, I use the same .htaccess code below in the main domain and it work like charm?

Here is my code in .htacess:

########## Begin - Redirecting non-www request to www
#
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule (.*) http://www.mydomain.com.my/$1 [L,R=301]
#
########## End - Redirecting non-www request to wwwthe

What should I do? Thanks & regards

phranque

9:57 am on Jul 19, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld [webmasterworld.com], cheyingtan!

i'm not clear from your post - is this .htaccess in the document root directory or in the joomla subdirectory?

that code looks like it should work in general, but you should probably add a couple of details to improve it such as a backslash to indicate a literal dot and anchoring regular expression in the RewriteRule:

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


do you have any other rewrites/redirects in the .htaccess or apache config file?

cheyingtan

10:18 am on Jul 19, 2010 (gmt 0)

10+ Year Member



Hello phranque,

Thanks for your reply.

the .htaccess file is in the subdirectory (I install the Joomla in the subdirectory. I have other thing in main root.)

So the url is www.mydomain.com/joomla, when user type in [mydomain.com...] i want it to redirect to [mydomain.com...]

I think i confuse on the code. Now my Joomla is in the sub directory, but I am using the code for the root.

In this case what 301 redirect code should i use? I tried the following but it doesn't work.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mydomain\.com/joomla/
RewriteRule ^(.*) [mydomain.com...] [L,R=301]

jdMorgan

2:27 pm on Jul 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In example.com/joomla/.htaccess:

RewriteEngine On
#
# Redirect requests for non-blank, non-canonical hostnames to canonical (www) hostname
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/joomla/$1 [R=301,L]

Jim