Forum Moderators: phranque

Message Too Old, No Replies

add www in front of the url with mod rewrite

         

valval

1:59 pm on Sep 4, 2005 (gmt 0)

10+ Year Member



Hi guys,

I was wondering if the following problem can be solved using Mod rewrite (and/or other Apache settings).

I have a Apache 1.3.3 box with two virtual hosts defined on it.

One looks like http://example.com/ the other one http://www.example.com ... they both point out to the same php site on the same server.

Basically I want a rewrite rule to force all users that type http://example.com/foo to actually go to http://www.example.com/foo ... is it possible?

(I know that there is a php script for that, but I rather not use it).

[edited by: jdMorgan at 2:07 pm (utc) on Sep. 4, 2005]
[edit reason] Example.com [/edit]

jdMorgan

2:06 pm on Sep 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



valval,

Wlecome to WebmasterWorld!

Have a look at these threads [google.com] for examples.

Jim

valval

3:12 pm on Sep 4, 2005 (gmt 0)

10+ Year Member



Ok, now I did realize that I have two different domains (one .org and one .com) on the same server, pointing to the same site, so http://example.com and http://example.org and I wanted them to have this redirection ...

I looked at the links you presented and I pasted the following into my .htaccess file

RewriteCond %{http_host}!^www\. [NC]
RewriteCond %{http_host} ^(.+)\.com [NC]
RewriteRule (.*) [%1.com...] [R=301,L]

RewriteCond %{http_host}!^www\. [NC]
RewriteCond %{http_host} ^(.+)\.org [NC]
RewriteRule (.*) [%1.org...] [R=301,L]

Now this does the job for all but a /forum/ directory (www.example.com/forum/ and www.example.org/forum/) which has it's own .htaccess rule (against santy and other worms and also some static url transformations).

Now basically the static url don't work anymore with http://example.com/forum/static_url/

They all get redirected to http://example.com/forum/ (which is then taken by index.php by default)

RewriteRule ^forum-([0-9]*)en.* index.php?f=$1&auto_lang=english
RewriteRule ^forum-([0-9]*).* index.php?f=$1
RewriteRule ^forums-en.html index.php?auto_lang=english
RewriteRule ^forums.* index.php

(this is a part of the rewrite rule)

Where should I place the paragraphs above in this .htacess file so that we first get the www in front, and afterwards the static_url is processed?

valval

4:12 pm on Sep 4, 2005 (gmt 0)

10+ Year Member



Nevermind, I got it :) I've added /forum/ on the second .htaccess file. Thanks a lot jdMorgan!

jdMorgan

4:24 pm on Sep 4, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could also "compress" the first two rulesets into one to save time:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.(com¦org) [NC]
RewriteRule (.*) http://www.%1.%2/$1 [R=301,L]

Note that posting on this forum changes the pipe "¦" character; Replace the broken pipe above with a solid pipe from your keyboard before trying to use this code.

Jim