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
Solved

http and https apache rewrite to include www

Posted on 2008-10-29
7
1,502 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DNS Question 7 69
Apache SSL and mod_rewrite not working 8 184
Problem to go to page 12 102
apache vhosts on mac not being recognized 5 92
Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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