Solved

http and https apache rewrite to include www

Posted on 2008-10-29
7
1,497 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
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
500 Internal Server Error When I Run Code Igniter 4 60
XAMPP 3 54
HHTP and HTTPS redirect question 3 97
Do you know any Office editor web for IIS or Apache web server)??? 10 80
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

867 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

22 Experts available now in Live!

Get 1:1 Help Now