LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (http://www.linuxquestions.org/questions/linux-server-73/)
-   -   Apache no-www to www domain rewrite after upgrade (http://www.linuxquestions.org/questions/linux-server-73/apache-no-www-to-www-domain-rewrite-after-upgrade-4175412263/)

deesto 06-19-2012 08:20 AM

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?

bathory 06-19-2012 03:29 PM

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?

deesto 06-19-2012 03:48 PM

Hi bathory,
Quote:

Originally Posted by bathory (Post 4707192)
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.

d3vrandom 06-19-2012 05:17 PM

Try this:

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

These work for any domain

deesto 06-20-2012 10:18 AM

Hi d3vrandom,
Quote:

Originally Posted by d3vrandom (Post 4707262)

Thanks; that's good stuff and shows how to manipulate requests to go both ways, which I can use in different situations.

deesto 06-20-2012 12:16 PM

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]
....



All times are GMT -5. The time now is 01:11 AM.