LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-19-2012, 08:20 AM   #1
deesto
Member
 
Registered: May 2002
Location: NY, USA
Distribution: FreeBSD, Fedora, RHEL, Ubuntu; OS X, Win; have used Slackware, Mandrake, SuSE, Xandros
Posts: 448

Rep: Reputation: 31
Question Apache no-www to www domain rewrite after upgrade


I have a simple rewrite to send requests not including 'www' in the domain to the appropriate domain URL, i.e., http://mydomain.com --> http://www.mydomain.com

After upgrading Apache from 2.2.3 to 2.2.15 (and a machine move, but the config remains the same), this rewrite no longer works and falls through to a default catch-all rule:

Code:
# redirect non-www. requests to the right place:
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteCond %{HTTP_HOST} !^www.mydomain.com$ [NC]
RewriteRule ^/.+www\/(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Did something change in the way Apache handles conditions, or is this rule screwed up to begin with?
 
Old 06-19-2012, 03:29 PM   #2
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Hi,
Quote:
Did something change in the way Apache handles conditions, or is this rule screwed up to begin with?
I don't think anything changed between those 2 apache versions.
Regarding your rewrite stuff, you can leave only 1 RewriteCond, as the other is redundant.
Also your rule is a bit strange. You want to rewrite just http://mydomain.com/www/foo to http://www.mydomain.com/foo? Or you want to rewrite anything from http://mydomain.com to http://www.mydomain.com?
 
1 members found this post helpful.
Old 06-19-2012, 03:48 PM   #3
deesto
Member
 
Registered: May 2002
Location: NY, USA
Distribution: FreeBSD, Fedora, RHEL, Ubuntu; OS X, Win; have used Slackware, Mandrake, SuSE, Xandros
Posts: 448

Original Poster
Rep: Reputation: 31
Hi bathory,
Quote:
Originally Posted by bathory View Post
Hi,

I don't think anything changed between those 2 apache versions.
I think you're right. I wonder why then that this works in an older instance and not in the newer.
Quote:
Regarding your rewrite stuff, you can leave only 1 RewriteCond, as the other is redundant.
Hmm, OK, I guess that's true! Thanks for that.
Quote:
Also your rule is a bit strange. You want to rewrite just http://mydomain.com/www/foo to http://www.mydomain.com/foo? Or you want to rewrite anything from http://mydomain.com to http://www.mydomain.com?
The latter: for anything without 'www' in the requested domain to be rewritten to its 'www' counterpart domain.
 
Old 06-19-2012, 05:17 PM   #4
d3vrandom
Member
 
Registered: Jun 2006
Location: Karachi, Pakistan
Distribution: OpenSUSE, CentOS, Debian
Posts: 59

Rep: Reputation: 9
Try this:

http://abdussamad.com/archives/100-Y...!-No-WWW!.html

These work for any domain
 
1 members found this post helpful.
Old 06-20-2012, 10:18 AM   #5
deesto
Member
 
Registered: May 2002
Location: NY, USA
Distribution: FreeBSD, Fedora, RHEL, Ubuntu; OS X, Win; have used Slackware, Mandrake, SuSE, Xandros
Posts: 448

Original Poster
Rep: Reputation: 31
Hi d3vrandom,
Quote:
Originally Posted by d3vrandom View Post
Thanks; that's good stuff and shows how to manipulate requests to go both ways, which I can use in different situations.
 
Old 06-20-2012, 12:16 PM   #6
deesto
Member
 
Registered: May 2002
Location: NY, USA
Distribution: FreeBSD, Fedora, RHEL, Ubuntu; OS X, Win; have used Slackware, Mandrake, SuSE, Xandros
Posts: 448

Original Poster
Rep: Reputation: 31
Actually, I found a minor problem with the code from the above article:
Code:
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
This results in a double-slash (//) being added between the domain and path in the URL. It's better to remove the extraneous slash:
Code:
...
RewriteRule (.*) http://www.%{HTTP_HOST}$1 [R=301,L]
....
 
  


Reply

Tags
apache, domain, rewrite, rewritecond, www


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bind9 all *.domain.com reachable, only www.domain.com isn't TonyDeWittePony Linux - Server 1 07-14-2011 01:03 PM
Apache https redirect without-www to with-www alitrix Linux - Server 3 07-24-2008 06:11 AM
DNS http:domain.com resolve to www.domain.com keysorsoze Linux - Networking 3 02-12-2007 03:03 AM
Cant get entry in bind to work with domain.com instead of www.domain.com pxes351 Linux - Networking 12 05-09-2005 06:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:11 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration