Forum Moderators: phranque

Message Too Old, No Replies

rewriting rule: cut www from hostname

how remove www prefix from all my hostname

         

xname

4:57 pm on Mar 24, 2006 (gmt 0)

10+ Year Member


Hello,
on my server with Red Hat Enterprice and Apache 1.34 I need to use Rewrite Module for a specific domain so that when one use the url:
http://www.account.domain.com it automatically changes in http://account.domain.it... I suppose this is possibile...but I don't know what is the exact rule and where to put it into httpd.conf.
Can you help me?
Thank you

moltar

5:04 pm on Mar 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, xname!

You can use the following rewrite rule to do what you want:

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

Where http://example.com/ is obviosly your domain :)

xname

5:13 pm on Mar 24, 2006 (gmt 0)

10+ Year Member



Hello moltar, thank you.

I was trying your suggested rule. It works well except for a double // at the end of the domain:
---> http://example.com//
I think it would be good to have only one / at the and, do you think that's possibile?

moltar

5:17 pm on Mar 24, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



:) Ya I guess it'd be better! I haven't tested this, I actually rewrote this from a reverse code that I use - to redirect from non-www to www version. I just like how www looks.

Just drop the / from the redirect, it should work, but you better test it.

Just for the reference, I know it's not what you want, but if someone else will be looking in this thread...

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

[edited by: jdMorgan at 1:28 am (utc) on Mar. 26, 2006]
[edit reason] Formatting. [/edit]

xname

5:25 pm on Mar 24, 2006 (gmt 0)

10+ Year Member



Perfect moltar. Here is the working rule you suggested:

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

jdMorgan

1:33 am on Mar 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note that the problem in this thread was caused by the fact that in a per-directory (.htaccess) context, the local URL-path 'seen' by RewriteRule is localized to the directory in which the .htaccess file resides. So the following code snippets do exactly the same thing:

In httpd.conf:


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

Alternate form for httpd.conf:


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

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