WebFaction
Community site: login faq
0
1

i want to redirect all requests like this:

www.site.com/*

to this (removing the www):

site.com/*

so far i do it like this:

for each of my apps i create an extra static app bound to 'www.site.com' containing only .htaccess file (which does a 301 redirect to site.com/*)

is there a cleaner solution?

thanks

asked 03 Jul '11, 05:43

junggle
1112
accept rate: 0%

edited 03 Jul '11, 05:49

I'd also want a clean solution for this. I've tried just using one app for all my sites, and add this to the .htaccess:

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

But that didn't work. I hope somebody has found a way.

(04 Jul '11, 12:30) bloodpet

That won't work, for a couple of reasons:

  • You're not capturing the base domain in your rewrite condition.
  • RewriteRule doesn't check the hostname, so your regex there won't match anything.

The trick is to add a second criteria to the rewrite condition to catch the domain, and then reference that with a % variable in the rewrite rule. See my answer below for a working example.

(04 Jul '11, 19:38) seanf

Assuming that the application mounted at www.site.com isn't already a PHP application that is the cleanest solution. If, however, the application is already PHP-based, you can simply add the redirects to your current .htaccess file.

permanent link

answered 03 Jul '11, 05:49

David L ♦♦
1.4k13
accept rate: 45%

Here's a generic rewrite that you can use rewrite any www subdomain to its parent domain.

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

So, with this you can:

  1. Create a single static/cgi/php app.
  2. Add that example to .htaccess for that app.
  3. Create a single website that serves that app for all of your www subdomains and redirects them to the appropriate site based on the subdomain of the incoming request.
permanent link

answered 04 Jul '11, 19:35

seanf
12.2k41836
accept rate: 37%

I haven't used rewrite like this before. Thanks for this. Now I'll have a neater list of apps.

(05 Jul '11, 02:16) bloodpet

perfect answer.

I especially love that you're doing this with a single rule for any domain.

(06 Jul '11, 15:11) swiharta
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×76
×75
×32
×10
×1

question asked: 03 Jul '11, 05:43

question was seen: 13,941 times

last updated: 18 Nov '15, 08:52

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM