logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Apache View previous topic :: View next topic
Reply to topic   Topic: Add www to domain unless it is a subdomain request
Author
NotionCommotion



Joined: 12 Nov 2014
Posts: 7

PostPosted: Tue 18 Nov '14 19:22    Post subject: Add www to domain unless it is a subdomain request Reply with quote

Hello!

I would like to rewrite mysite.com to www.mysite.com.

However, if the request is a subdomain (i.e. blabla.mysite.com), then it should not rewrite.

I believe this gets me close, but it will not differentiate the subdomains (ie blabla.mysite.com).

Code:
<IfModule mod_rewrite.c>
   RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
   RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
 </IfModule>


Thank you
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7373
Location: Germany, Next to Hamburg

PostPosted: Wed 19 Nov '14 15:06    Post subject: Reply with quote

You can put that into your vhost. Than it will be only for that domain
Back to top
NotionCommotion



Joined: 12 Nov 2014
Posts: 7

PostPosted: Wed 19 Nov '14 22:14    Post subject: Reply with quote

Thanks James,

I wasn't clear. I am using serverAlias (http://httpd.apache.org/docs/2.2/mod/core.html#serveralias) where mysite.com and any subdomain go to the same vhost. I wish to rewrite mysite.com to www.mysite.com, but not rewrite blabla.mysite.com, etc.

Code:
<VirtualHost *:80>
    ServerName mysite.com
    ServerAlias *.mysite.com
    DocumentRoot /var/www/html
</VirtualHost>
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7373
Location: Germany, Next to Hamburg

PostPosted: Thu 20 Nov '14 11:31    Post subject: Reply with quote

Change your rule to check if the site is mysite.com
Back to top
NotionCommotion



Joined: 12 Nov 2014
Posts: 7

PostPosted: Thu 27 Nov '14 15:29    Post subject: Reply with quote

How is this accomplished?

I am trying the following, and it redirects over and over again.

Code:
RewriteCond %{HTTP_HOST} !^www\.mysite\.com.+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


Ideally, I would not have to specify "http" so that if I use this on a 443 https virtual host, it will still work, but instead use some variable which is equal to the original request.

Also, it would be nice not to have to hardcode mysite.com so that if I change the primary domain, it will still work.

Thanks
Back to top
NotionCommotion



Joined: 12 Nov 2014
Posts: 7

PostPosted: Thu 27 Nov '14 21:24    Post subject: Reply with quote

Ended up creating a virtual server for mysite.com which redirects to www.mysite.com, and a second virtual server for .mysite.com.
Back to top


Reply to topic   Topic: Add www to domain unless it is a subdomain request View previous topic :: View next topic
Post new topic   Forum Index -> Apache