Forum Moderators: phranque

Message Too Old, No Replies

htaccess permanent redirect

How do I redirect http://domain.tld to http://www.domain.tld?

         

Moncao

7:36 am on Aug 30, 2006 (gmt 0)

10+ Year Member



Hi

Is it possible to redirect //domain.tld to //www.domain.tld in the htaccess file? I tried but it gave my server an almight hiccup.

coopster

3:34 pm on Aug 30, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes, it is. For any given directive, all you have to do is have a look at the Context [httpd.apache.org] to determine where in the server's configuration files the directive is legal. link [httpd.apache.org] can be used in per-directory override (
.htaccess
) files.

Have a look at your error logs to see what your issue may be.

Moncao

6:49 am on Aug 31, 2006 (gmt 0)

10+ Year Member



Thanks Coopster

Any chance you could just give me the code to try?

coopster

8:29 pm on Aug 31, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



How about giving it your best effort first? I'll bet you can figure out ;)
If not, let us know and we'll help you tweak things.

Redirect [httpd.apache.org]

Moncao

6:57 am on Sep 1, 2006 (gmt 0)

10+ Year Member



I have tried
redirect permanent [domain.tld...] [domain.tld...]
I do not see how else I can do this using the htaccess file

smells so good

7:48 am on Sep 1, 2006 (gmt 0)

10+ Year Member



There is a real good primer for redirects at this page: [webmasterworld.com...]
It is absolutely worth the time it will take to read.

Is your Rewrite Engine on? It's difficult (for me) to give you a definitive answer without knowing how your file is already configured. You should be able to use something similar to the following code.

RewriteEngine on
RewriteRule ^domain.tld$ http://www.domain.tld [R=301,L]

Permament redirects are common and should not cause any problems on your server. The Apache Web Server Forum [webmasterworld.com] has a Library [webmasterworld.com] full of information.

coopster

6:18 pm on Sep 1, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The problem with your redirect, Moncao, is that the old URL-path should be a case-sensitive (%-decoded) path beginning with a slash, not a full URL with scheme and hostname.
redirect permanent / http://www.domain.tld

However, this is not the ideal way to handle the redirection as it may not always be necessary (such as when the user has already entered in the correct hostname). Optionally, you could use a rewrite as suggested. The Apache forum is loaded with responses similar to your issue, a quick search should turn up quite a few discussions.

jdMorgan

6:34 pm on Sep 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In order to prevent a redirection loop if both domain variants resolve to the same server, you'll need to use RewriteCond to test the requested hostname:

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

Jim

Moncao

2:01 pm on Sep 2, 2006 (gmt 0)

10+ Year Member



coopster

What you said puts the server into a spin and eventually brings up a page not found.

jdMorgan/ smells so good

Sorry to be such a dummy, but I am on such matters. Do I just put that script in the htaccess file?

[edited by: Moncao at 2:07 pm (utc) on Sep. 2, 2006]

coopster

6:56 pm on Sep 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yes.


What you said puts the server into a spin and eventually brings up a page not found.

Only if the two domains resolve to the same server.

Optionally, if the domains are on the same server and you have access to the conf directly you could set the redirect in a separate <VirtualHost> container. Now you won't have to invoke mod_rewrite on every request to your domain.

Moncao

7:18 am on Sep 3, 2006 (gmt 0)

10+ Year Member



coopster

"Optionally, if the domains are on the same server and you have access to the conf directly you could set the redirect in a separate <VirtualHost> container. Now you won't have to invoke mod_rewrite on every request to your domain."

I am sorry to be so dumb but what you wrote might as well be in Swahili. I have a virtual account and can find nothing about virtualhost or containers in my cpanel. I am sorry, you seem to think everyone has their own server and knows about matters past html and electricity. I am simple webmaster who wants to get over a problem with Google. All the threads and explanations I get are gobbly gook to me. The original question was about whether it is possible to do what I want in the htaccess file; clearly it is not after all that. Surely someone can relate to me and knows about normal cpanels and whether and where it is possible to do what I want? If not, if it is a matter for the web hosting company, please someone tell me what to write to them?

PS I do not have two domains.

[edited by: Moncao at 7:19 am (utc) on Sep. 3, 2006]

smells so good

8:17 am on Sep 3, 2006 (gmt 0)

10+ Year Member



>> Sorry to be such a dummy, but I am on such matters. Do I just put that script in the htaccess file?

Swahili? Nope, it's Apache. :) To answer your question, yes, you simply type those lines into your .htaccess file. It isn't a script, per se, it's a directive to the Apache server, telling it to redirect all requests made to domain.tld permanently to www.domain.tld (be sure to replace domain.tld with your own domain information).

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

Moncao

8:26 am on Sep 3, 2006 (gmt 0)

10+ Year Member



smells so good

Perfect, thank you very, very much.

smells so good

9:56 am on Sep 3, 2006 (gmt 0)

10+ Year Member



Give thanks where they're due, to jdMorgan and coopster, for not letting me lead you too far astray. Glad you got it working.