Forum Moderators: phranque
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]
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
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]
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]
[edited by: jdMorgan at 4:26 am (utc) on Oct. 2, 2005]
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]
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
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]
Options +FollowSymLinks
RewriteEngine on
#
# Redirect to preferred www domain
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Jim
[edit] Corrected per Message #29 [/edit]
[edited by: jdMorgan at 7:58 pm (utc) on Oct. 31, 2005]
I am taking it that this line with the # I do not change?
# Redirect to preferred www domain
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
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>
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. :)
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
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
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]
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.
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
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!