Solved

http and https apache rewrite to include www

Posted on 2008-10-29
7
1,496 Views
Last Modified: 2012-06-27
This is what I am currently using and it appears to work for non-SSL pages.  The goal here was to ensure a prepended www to domain names (www.domain.com), but NOT to sub domain names (www.subdomain.domain.com).

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

I would like to do this exact same thing for https as well but don't know how???

PS: If you see a cleaner way to do the above feel free to advise.
0
Comment
Question by:matt1237
  • 4
  • 3
7 Comments
 
LVL 27

Expert Comment

by:caterham_www
ID: 22833669
You're using the rule-set in your httpd.conf? Put the same into your <virtualhost...> for port 443 but change http:// into https://

You may change

RewriteCond %{HTTP_HOST} !^$

into
RewriteCond %{HTTP_HOST} !=""

as well, which is the same but a literal comparison instead of a regular expression.
0
 
LVL 2

Author Comment

by:matt1237
ID: 22833940
I like the idea of using !="" since I'm guessing it would be faster not having to do regular expressions.  Was that your point?
More Explanation:
These two files are needed for each virtual host on my GoDaddy dedicated Fedora server.  I am using Plesk to make things a little easier since I have over a hundred domains.
  • httpd.include (includes default info for each domain and I cannot modify it since any changes elsewhere will cause it to overwritten)
  • vhost.conf (this file is where all changes are made to either overwrite things in httpd.include or whatever I want to do in addition.  What you see in the first post is what is already in this file)
Both port 80 and 443 are using the same directory and I don't believe I have to write out <VirtualHost insert-IP-address-here:443>...</VirtualHost>
anywhere in vhost.conf file... is there another way to do this?  I've already tried this and it worked for http but not https:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^\w+\.\w+\.\w+ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^\w+\.\w+\.\w+ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}$1 [R=301,L]
0
 
LVL 27

Accepted Solution

by:
caterham_www earned 500 total points
ID: 22834417
> Was that your point?

Yes.

I think your vhost.conf is not included into the <virtualhost...> for port 443, which is generated by plesk.

> I don't believe I have to write out <VirtualHost insert-IP-address-here:443>...</VirtualHost> anywhere in vhost.conf file

No, that won't work since vhost.conf is included into the <virtualhost> for port 80 in your httpd.include. You may open and examine "httpd.include" both for <virtualhost... and a directive called 'Include'. May be you'll find a virtualhost for port 443 with an Include directive pointing to some other file (may be vhost_ssl.conf).

If there's no Include file for the port 443 virtual host in httpd.include, you may create a file called 'vhost_ssl.conf' and call plesk via

/usr/local/psa/admin/sbin/websrvmng --reconfigure-vhost --vhost-name=<domain_name>

(may be with a different path). Plesk should recognize the file vhost_ssl.conf and should create an Include directive in httpd.include.

---
RewriteCond %{HTTPS} off  <-- that would be a regular expression, too,
RewriteCond %{HTTPS} =off   not.
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 2

Author Comment

by:matt1237
ID: 22834494
You're a genious!  Thank you for the optimization and pointers.  I didn't know about vhost_ssl.conf before.  Once that was created, everything worked beautifully.  Essentially that file does the http/https conditioning for me so it's very easy to separate code for the two.
0
 
LVL 2

Author Comment

by:matt1237
ID: 22834582
Here is the file code for vhost.conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^\w+\.\w+\.\w+ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
<Directory /var/www/vhosts/domain.com/httpdocs>
php_admin_value open_basedir "/var/www/vhosts/domain.com/httpdocs:/tmp"
Options +FollowSymLinks
</Directory>
And final code for vhost_ssl.conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^\w+\.\w+\.\w+ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}$1 [R=301,L]
<Directory /var/www/vhosts/domain.com/httpdocs>
php_admin_value open_basedir "/var/www/vhosts/domain.com/httpdocs:/tmp"
Options +FollowSymLinks
</Directory>  
0
 
LVL 2

Author Comment

by:matt1237
ID: 22834596
I wrote this line myself to make sure no subdomains get "www" appended to them.  It seems to work, but is this an ok way to do it?
RewriteCond %{HTTP_HOST} !^\w+\.\w+\.\w+ [NC]
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 22834869
Yes, it should work in most cases. The RegEx \w does hot match a hyphen ('-')  (but '0-9a-z_'). A '-' is a valid character in a HTTP_HOST, too. But if you don't use such a character, that's not an issue, of course.
The condition will also match a.b.c.d.e.f (multiple levels). If that is not intended, you may use

^[^.]+\.[^.]\.[a-z]{2,4}\.?(:[0-9]{2,4})?$

[^.] = anything but not a period

The RegEx will take care of
foo.example.com
foo.example.com.
foo.example.com:80
foo.example.com.:80

which are all valid HTTP_HOSTs.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
Hi, in this article I'm going to teach you how to run your own site, and how to let people in (without IP). I'll talk about and explain each step... :) By the way, everything in this Tutorial is completely free and legal. This article is for …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now