Keep Server Online
If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.
or
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.
| |
|
Topic: Add www to domain unless it is a subdomain request |
|
Author |
|
NotionCommotion
Joined: 12 Nov 2014 Posts: 7
|
Posted: Tue 18 Nov '14 19:22 Post subject: Add www to domain unless it is a subdomain request |
|
|
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
|
Posted: Wed 19 Nov '14 15:06 Post subject: |
|
|
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
|
Posted: Wed 19 Nov '14 22:14 Post subject: |
|
|
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
|
Posted: Thu 20 Nov '14 11:31 Post subject: |
|
|
Change your rule to check if the site is mysite.com |
|
Back to top |
|
NotionCommotion
Joined: 12 Nov 2014 Posts: 7
|
Posted: Thu 27 Nov '14 15:29 Post subject: |
|
|
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
|
Posted: Thu 27 Nov '14 21:24 Post subject: |
|
|
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 |
|
|
|
|
|
|