Forum Moderators: phranque

Message Too Old, No Replies

Redirect non www

Using .htaccess

         

hunkydory

11:28 am on Aug 22, 2005 (gmt 0)

10+ Year Member



Hello!

I have been looking for a way to re-direct traffic from: http://example.com to http://www.example.com . I have found many different scripts doing this and was wondering which is the most effective way?

Thx for any help

[edited by: jdMorgan at 6:47 pm (utc) on Aug. 22, 2005]
[edit reason] Examplified. [/edit]

jdMorgan

1:39 pm on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hunkydory,

It would be most effective if you were to tell us more about your situation and post one of these "many scripts" and ask a specific question. Do you know if you can use mod_rewrite on your server? Do you have an existing .htaccess file? Do you have access to the httpd.conf configuration file?

Jim

gstiles

6:33 pm on Aug 22, 2005 (gmt 0)

10+ Year Member



Jim

I too am wondering what is the "best" approach to doing this type of redirect. I don't have any "cool scripts", but we do have an existing .htaccess file and I can modify the http.conf file and use mod_rewrite if necessary.

Thanks,

Gannon

curlykarl

7:13 pm on Aug 22, 2005 (gmt 0)

10+ Year Member



You can use this in your .htaccess file

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

Works for me on unix hosting.

Karl

[edited by: jdMorgan at 1:24 am (utc) on Oct. 2, 2005]
[edit reason] Example.com [/edit]

hunkydory

9:07 am on Aug 30, 2005 (gmt 0)

10+ Year Member



Sorry for the slow reply thanks that worked :)

caspita

6:47 pm on Sep 30, 2005 (gmt 0)

10+ Year Member



I wasn't worry about duplicates in my web-site until I did the GG command:

site:exmaple.com -www

and 500 pages were returned!

now I want to implement the redirect from curlykarl above but I'm not sure about modifying my .htaccess file in this case.

This is what I have in my .htaccess file:

========================
RewriteEngine On
RewriteRule html_file.html html_file.html [L]
RewriteRule ^(.*)\.html$ /index.php?html_page=$1 [L]
========================

Note: The second line RewriteRule html_file.html html_file.html [L], is beacuse I have a 'single' html page that I do not want to be rewrite with the last rule.

Now, I'm not sure how my .htaccess will look when the redirect so I don't mess up mi site access. I was thinkig somethig like this:

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

RewriteRule html_file.html html_file.html [L]
RewriteRule ^(.*)\.html$ /index.php?html_page=$1 [L]
=========================

Will that be the correct place to put the 2 lines and keep the site working fine?, of course I'll test it but I just want to confirm with the experts.

Thanks,
CS.

[edited by: jdMorgan at 1:25 am (utc) on Oct. 2, 2005]
[edit reason] Example.com [/edit]

jdMorgan

9:43 pm on Sep 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd recommend:

RewriteEngine On
#
# Redirect to preferred www domain
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Rewrite all requests except html_file.html to index.php
RewriteCond %{REQUEST_URI} !^/html_file\.html$
RewriteRule ^([^.]+)\.html$ /index.php?html_page=$1 [L]

Jim
[edit] Correct typo. [/edit]

[edited by: jdMorgan at 4:26 am (utc) on Oct. 2, 2005]

caspita

1:52 am on Oct 2, 2005 (gmt 0)

10+ Year Member



Thanks Jim,

non-www to www and redirect to index.php are working fine, but looks like the exception for no redirecting the html_file.html is not working I get the request passed to the index.php also.

CS.

jdMorgan

4:26 am on Oct 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



See the correction above -- I typoed "REQUEST_URI".

Also, be sure to flush your browser cache before testing.

Jim

caspita

11:15 am on Oct 2, 2005 (gmt 0)

10+ Year Member



working now thanks Jim!

stuff 4 beauty

6:08 pm on Oct 17, 2005 (gmt 0)



I need major help - please if someone could help me.

I need to redirect my non www domain to my www.domain

I created an htaccess file, but do not know what to write in it. Is there someone out there that can help me so I don't mess my whole site up?

I appreciate anyone's time with my problem

Thanks

stuff4beauty

[edited by: jdMorgan at 6:33 pm (utc) on Oct. 17, 2005]
[edit reason] No URLs or sigs, please. See TOS. [/edit]

jdMorgan

6:39 pm on Oct 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



stuff4beauty,

Welcome to WebmasterWorld!

The code is posted in msg#7 above (first five lines as commented). However, I don't recommend you get into this without some study. Mod_rewrite is extremely powerful and therefore potentially dangerous; A single-character change or a typo can radically alter the function of a rule set. The result can be to take your server down immediately, or --far worse-- to create a 'silent' error, one that only manifests itself occasionally and causes a very-hard-to-troubleshoot failure. So, I cannot recommend that anyone cut-n-paste code onto their server without understanding exactly how it works.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

stuff 4 beauty

7:09 pm on Oct 17, 2005 (gmt 0)



Thanks for the welcome and the reply! I would be happy to hire someone and pay someone to do this for me. But I guess I could try to do it.

So would I copy the code (below at the bottom) into my .htaccess file and replace my non www in example\.com and put my www into http://www.example.com/

would I need to use this bottom part of the code?

# Rewrite all requests except html_file.html to index.php
RewriteCond %{REQUEST_URI}!^/html_file\.html$
RewriteRule ^([^.]+)\.html$ /index.php?html_page=$1 [L

RewriteEngine On
#
# Redirect to preferred www domain
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Rewrite all requests except html_file.html to index.php
RewriteCond %{REQUEST_URI}!^/html_file\.html$
RewriteRule ^([^.]+)\.html$ /index.php?html_page=$1 [L]

jdMorgan

7:28 pm on Oct 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are concerned only with these lines, as indicated by the comments in the code:

Options +FollowSymLinks
RewriteEngine on
#
# Redirect to preferred www domain
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Using the links above, take that code apart character-by-character to find out how it works. The first line may or may not be needed on your server. It can cause a problem if missing and needed, or if present and not needed -- this depends on your particular server configuration.

Jim

[edit] Corrected per Message #29 [/edit]

[edited by: jdMorgan at 7:58 pm (utc) on Oct. 31, 2005]

stuff 4 beauty

7:33 pm on Oct 17, 2005 (gmt 0)



I will try it and do really appreciate your time and help

stuff 4 beauty

7:44 pm on Oct 17, 2005 (gmt 0)



I tried it all ways and none of them do anything at all to solve my problem.

I am taking it that this line with the # I do not change?

# Redirect to preferred www domain

stuff 4 beauty

8:07 pm on Oct 17, 2005 (gmt 0)



OK - I tried it again and again and you are great! It worked! Thank you SOOOoooo much!

ann

10:47 pm on Oct 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

My first time in here. I have been reading but still not sure what to do and would really appreciate your help.

My domains options are:

option includes IncludesNOEXEC (if 2 words for includes is simply a typo That is the way I received it)
<Directory /home/mydomain.com>
AllowOveride All
Options +includes
Order allow,deny
Allow from all
</directory>

I need to do a redirect from no www to www.domain.com

Is one of the two above right for me?

Thank you in advance

jdMorgan

1:18 am on Oct 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That "option" directive was syntactically incorrect, in that the correct directive is "Options" - plural. As a result, it probably didn't do anything, and you can probably comment it out or delete it.

Since you're using a <Directory> container, I assume that this code came from your httpd.conf file, and not from an .htaccess file. In that case, you'll need a very slight change to the code posted in msg#14 above in order to work properly in httpd.conf. Rolling it all together:


# option includes IncludesNOEXEC (commented-out invalid 'option' directive)
#
<Directory /home/mydomain.com>
AllowOveride All
Options +Includes +FollowSymLinks
Order allow,deny
Allow from all
#
RewriteEngine on
# Redirect to preferred www domain
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^/(.*) http://www.example.com/$1 [R=301,L]
</Directory>

You will need to restart the server for changes to httpd.conf to take effect, and flush your browser cache so that it doesn't interfere with testing the new code. I strongly recommend that you make a backup of the existing file before changing it. I'd hate to take your site down overnight because I made some small typographical error above... :o

Jim

ann

2:24 am on Oct 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim,

Thank you for helping but I should have made myself clearer, I do not have access to the server. This is what I was given when my domain was set up on the dedicated virtual server, I just want to know how to redirect non www to www and I am afraid to monkey with it as the one time I tried I had my website wanting to download rather than open so I quickly loaded an empty htaccess file over it.

In my ftp client, and I think it is so with Apache, I cannot "see" the file unless I have a shell account, which I don't.

SO, what I wrote there was simply the configuration of my 'space' or directory, on the server as supplied in an email from the host. Hope I am not confusing you as badly as I am confused. :)

jdMorgan

2:33 am on Oct 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, try the code in msg#14 above, placing it into a plain-text file named ".htaccess" in the "home page" directory of your site.

It may or may not work, depending on your server configuration -- I'm quite sure that the stuff they sent you (typo-bug and all) is not *all* of your configuration settings; It take much more than that to start the car, as it were...

Jim

jd01

2:38 am on Oct 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your permissions should be OK to run mod_rewrite in the .htaccess. In most FTP clients, you can send a -LA command (usually in 'custom settings') which will show you files that are normally invisible.

The actual code you will use will be slightly different from the code jdMorgan provided... you will need to remove the preceding / from the rule, like this:

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

Make sure when working with your .htaccess you save the file as plain text (Not rich, etc.) and that you upload in ASCII mode (most FTP clients default to this, for .htaccess/.txt files but good to be sure.)

If you have not already, I recommend visiting the Apache Library [webmasterworld.com] and Forum Charter [webmasterworld.com] to get a better idea of mod_rewrite.

Hope this helps.

Justin

ann

5:05 am on Oct 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you both for helping :)


Just to be on the safe side. Is this what I am supposed to use?

Options +FollowSymLinks
RewriteEngine on
#
# Redirect to preferred www domain
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]

[edited by: jdMorgan at 7:59 pm (utc) on Oct. 31, 2005]
[edit reason] Corrected per message #29 [/edit]

jdMorgan

8:11 pm on Oct 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As above, please see message #14...

Jim

ann

10:54 pm on Oct 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I thank you guys from the bottom of my heart. You have made my day! IT WORKS!

Thank you, thank you, thank you.

BradStevens

8:40 pm on Oct 30, 2005 (gmt 0)

10+ Year Member



Greetings oh knowldegable ones. I've really, really tried to do my research and let me apologize in advance for this "help me" post, but I've tried to alter my .htaccess file to redirect the non www and have failed miserably.

I host on a virtual host. Here is my current .htaccess file:

# -FrontPage-

IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName example.com
AuthUserFile /usr/local/apache/example/_vti_pvt/service.pwd
AuthGroupFile /usr/local/apache/example/_vti_pvt/service.grp

Can someone let me know what I need to do to alter this code to redirect my non www?

Many blessings for your expertise.

jdMorgan

1:40 am on Oct 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



BradStevens,

Welcome to WebmasterWorld!

> I've tried to alter my .htaccess file to redirect the non www and have failed miserably.

If you need help with your code, and would like to learn how and where you might have gone wrong, please post it for discussion. Otherwise, all I can suggest is to add the code in message #14 above to the end of what you've got.

If you are actually using the FrontaPage extensions on your server, then see also this thread: Apache mod_rewrite and FrontPage extensions [webmasterworld.com]

Jim

BradStevens

5:22 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



Ok, I've read my way through this thread as well as the Frontpage thread previously referrenced and have rewritten the .htaccess file to read as follows:

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

I have also rewritten "Options None" to "Options +FollowSymLinks" within the following files:
_vti_bin ; _vti_bin /_vti_adm ;& _vti_bin/ _vti_aut

I've FTP'ed the files in and my website works fine ... no errors. No problems accessing via FrontPage. No problems with forms, even those that submit results via e-mail.

However, if I access example.com (non www) in my browser, it still does not seem to redirect to www.example.com.

Anyone have a clue?

Thanks much for your help. I'm reading as much as I can ... really!

hutchins13

5:45 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



If you copy the code from either messages #14 or #23, change the "0" (zero) used to spell "0ptions" to an "O", "Options". Otherwise, you will get an error. I hope that is clear.

BradStevens

6:18 pm on Oct 31, 2005 (gmt 0)

10+ Year Member



Thanks. I have used an "O" and not a zero. I am receiving no errors whatsoever. However, the redirect still just does not seem to work.

I hate it when I can't get something to work!

This 32 message thread spans 2 pages: 32