Forum Moderators: phranque

Message Too Old, No Replies

How to make all URLs of domain.com => www.domain.com

         

Nicke

8:53 pm on Mar 5, 2004 (gmt 0)

10+ Year Member



How to convert URLs of [domain.com...] into www.domain.com instead?

I dont like to have different PR values depending on with or w/o www as a part of the URL. I know some people link to me without www but I cant do anything about and thought rewrite rule may be the solution.

bcolflesh

8:56 pm on Mar 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteCond %{HTTP_HOST}!^www\.domain\.com [NC]
RewriteRule ^(.*) [domain.com...] [R]

jdMorgan

9:02 pm on Mar 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Nicke,

Try this in your Web root directory .htaccess file:


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

Jim

warmonger

8:18 pm on Mar 11, 2004 (gmt 0)

10+ Year Member



My hosting server uses canonical names. In my quest to stop receiving 301 and 302 errors in my web stats, My domain has been in existence for over a year, but only a construction page existed on the main page until a couple of months ago.

I recently put the following code in my httaccess file. Can you tell me if I was correct in doing this, or if there is a better way of doing it.

RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.site-name\.com
RewriteRule (.*) [site-name.com...] [R=301,L]

RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.site-name\.com
rewriterule (.*) [site-name.com...] [R=permanent,L]

The 302 errors were my biggest problem, and putting the [R=Permanent] code seemed to stop that. A day ago, I put in the [R=301] but I wanted to make sure I was correct in putting both of these in or if there is a different/better way to write this

Be gentle. I'm very new to Apache servers, as well as this site. Although I'm quite knowledgeable, this site is like a gold mine of the minds, it's almost scary. (That's why I've been lurking for about a month) :-)

Thanks,

Ken

closed

4:48 am on Mar 12, 2004 (gmt 0)

10+ Year Member



This should work:

RewriteEngine on
RewriteCond %{HTTP_HOST}!^www\.site-name\.com [NC]
RewriteRule (.*) http://www.site-name.com/$1 [R=301,L]

301 and permanent mean the same thing in the code.

woro2006

1:51 pm on Mar 12, 2004 (gmt 0)

10+ Year Member



I personally prefer to use the following
# changes example.com to www.example.com
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteRule ^(.*) [example.com$1...] [R=301,L]

warmonger

9:10 pm on Mar 12, 2004 (gmt 0)

10+ Year Member



I didn't realize I only needed only one of them and not both.

Thanks,

Ken

closed

5:06 am on Mar 13, 2004 (gmt 0)

10+ Year Member



You're welcome, Ken.

Nicke

11:30 am on Mar 16, 2004 (gmt 0)

10+ Year Member



Thanks for the help.

Obviously this Apache server don't have mod_rewrite module and he told me he had problem to compile it when he tried. (On request from me)

It is possible to do the same thing from the master config file of the Apache server without mod_rewrite module?

py9jmas

11:45 am on Mar 16, 2004 (gmt 0)

10+ Year Member



If you can set up a server on example.com (usually by setting up a virtual host):

For the main Apache config:

<VirtualHost *>
ServerName example.com
Redirect permanent / [example.com...]
</VirtualHost>
<VirtualHost *>
ServerName www.example.com

*** Insert the server config for your actual site ***

</VirtualHost>

Jon

enotalone

1:02 pm on Apr 21, 2004 (gmt 0)

10+ Year Member



Morgan's script works fine on its own. however, some mod_rewrite setting i have in other directories in side .htaccess files do not work when i put Morgan's script in the root.

those mod_rewrite setting i have in other .htaccess files in second level directories are supposed to convert dynamic urls into static and look like this:


RewriteEngine on
RewriteBase /books/
RewriteRule ^$ /books3.php?mode=books&node=1000&node2=45&col=4 [NE,L]
RewriteRule images/(.*).html /image.php?ASIN=$1 [NE,L]
RewriteRule (.*).html /book3.php?ASIN=$1&zcat=books [NE,L]

1. any idea why Morgan's mod_rewrite conflicts with the mod_rewrite above?

2. how would Morgan's mod_rewrite be altered to rediret just index.php to www.domain.com if the user is accesssing through domain.com?

Stefan

10:44 pm on Apr 23, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim, I know I should take the time to learn all about mod rewrites etc, but I really don't have it. I had a travel magazine link to our site recently without the www, the first site that has done this, and I need to finally break down and take care of this stuff, (also, two weeks ago I moved the site to an Apache server, from Windows, so now it's possible).

If I just copy and paste that code you posted, with the domain name changed of course, into the htaccess that I created after the server move, (to do some successful 301 redirects), will it work or do I need to tweak it somehow?

Sorry for my relative ignorance, man... if I ever get the chance, I'll study this stuff in detail.

jdMorgan

1:41 am on Apr 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Stefan,

The code I posted above is generic. It'll redirect any request with a hostname other than what is specified in the RewriteCond. The hostnames in the first and second lines must match - the first one (in the RewriteCond) with a "!" in front of it (meaning NOT), and properly anchored and escaped as a regular-expressions pattern, the second (in the RewriteRule) as plain-text followed by a back-reference to the requested local pathname ($1).

Some of the code above has anchoring errors -- missing start anchors to be specific. mod_rewrite will run faster if you use start and end anchors where possible (and if needed). Don't end-anchor the hostname in the RewriteCond, though -- it can cause problems if a user-agent appends a port number, as in www.example.com:80/index.html

There's a pretty simple reason we don't want to write code for people in this forum -- We'd rather teach people to fish and feed them for a lifetime, than give them a fish and feed them for only a day. Someone without any understanding of how the code works will be right back tomorrow with another problem or with a request for a new feature. I can't handle it all, even with the generous help of the many other members who contribute here... Not to mention the fact that we all need some time to make a living.

So we ask everyone to read the mod_rewrite documentation and the Apache URL Rewriting Guide, and to post their "best-effort" code. By the time they get to that point, it's usually only a minor point that needs to be improved, or a short discussion to get over some small misunderstanding. Occasionally, I'll break that rule and post some code, especially when the subject is particularly interesting or if it might help a large number of people. Other contributers may do the same, and that's OK, too. But we can't do it too often, or the hungry will quickly overwhelm the fishermen.

So anyway, you might call this a school, but not a free code-writing shop (I charge for that, as do many others here). If you don't have any time to study, try a WebmasterWorld site search on Google for what you want to accomplish, plus the word "RewriteRule" -- that'll usually turn up something. Or post over in the Commercial Exchange forum for bids on the work. I hope that clarifies the issue a bit -- and that is my sole intent here, to clarify; We all have different priorities, and that is understandable.

Here's a free tip for all: Add the following code to the "Custom code insert top" box in your WebmasterWorld control panel:

<center><p> <form method="GET" action="http://www.google.com/search"><input type="hidden" name="domains" value="webmasterworld.com"><input TYPE="text" name="q" size="25" maxlength="255" value><input type="submit" name="btnG" value="Google"><input type="radio" name="sitesearch" value="webmasterworld.com" checked>webmasterworld.com<input type="radio" name="sitesearch" value>www</form></center>

Poof! -- Instant site search (and Web search, too.)

Jim

Stefan

2:13 am on Apr 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks, Jim. I understand entirely.

For the time being, I'll probably assume that the one link without a www won't cause any great damage. I have a massive amount of work to be done wrt the raison d'etre for the site, (enter much data into an .xls, proofread other's notes, begin planning for the next expedition, submit another research application, continue planning for a 7 day transect, through very rough terrain, this Aug in assistance to a bunch of biologists...), and I have to find more funding, (which is why I can't pay for assistance).

Eventually, I will find time to study Apache server mod code. I like a challenge.

Stef

Stefan

1:17 am on Apr 26, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I read up on things a little, tried that code in the htaccess, and it seems to work fine... [example.org...] magically turns into [example.org...] when you hit enter on the browser.

It also looks good in the logs:
"GET / HTTP/1.1" 301
"GET / HTTP/1.1" 200

Thanks, man.