Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

http and https apache rewrite to include www

Posted on 2008-10-29
7
Medium Priority
?
1,519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
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

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

705 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