Flux RSS des billets

DotMG's joblog

Work hard at whatever you do! (Ecc. 9. 10a)
La catégorie apache a 2 Billets
Publié le 8 Oct 2013, 6:42 am dans apache

I recently purchased a wildcard SSL for my domain *.dot.mg. I have many subdomains that I wanted also to serve as https.

Once the SSL installed and fully functional, accessing to any of the subdomains leaded to the main site, because SSL configuration is per IP address, and not per domain name. But the solution is very simple, and in 3 lines. It is done by proxying requests to the main site to the non-https version. So, visitor connectes via https to https://blog.dot.mg/, but this request is directed to my main site https://dot.mg/. It will be the responsibility of https://dot.mg/ to redirect internally this request into http://blog.dot.mg/

It is important to know the difference between normal redirection and proxy redirection. With normal redirection, when a visitor goes to https://blog.dot.mg/, he is asked to go to http://blog.dot.mg instead, so he will load the unsecure version of the site. On the other hand, with proxy redirection, it is the webserver who will load the unsecure version of the site, then change all references to http into https and give the modified html page to the visitor. Thus, the communication between the client and the server is secure

 RewriteCond %{HTTP_HOST} ^blog.dot.mg$
 RewriteCond %{HTTPS} =on
 RewriteRule ^(.*)$ http://blog.dot.mg/$1 [P,L]

As simple as that.

The first lines filters the rewriting rule only for the subdomain blog.dot.mg. The second applies only the rule for HTTPS. The third line does the proxy redirection. Note the flag P instead of R. It is the only trick that makes it possible to view the site through https.

Publié le 4 Oct 2012, 8:15 am dans apache, php

This is the second part of the story about this site in all plain html that needed some revamp. Yesterday, I talked about installing PunBB as a forum, and today, I'll talk about a quick templating.

I think the webmaster used tools like Dreamweaver to build the site. My friend asked me to move one image in the footer of the page. That image was inside a <table>, moving it for one page was not a PITA, but repeating the steps for all .html files would really suck. Other options would be to create a script to do this automatically, or restart from scratch with a templated system. Finally, I found a dirty quirk to do the job, and without changing any file present in the site. It's really simple, I redirect all URL in .html into a script mytemplate.php, passing the filename as a parameter, and do everything in mytemplate.php.

.htaccess to perform the redirection:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^([^/]*).html$ mytemplate.php?page=$1 [L]
RewriteRule ^/?$ mytemplate.php?page=index

Second step is to copy index.html into mytemplate.php. I stripped all content, leaving only what would be in a template, ie: <head>, header, footer and sidebar part of <body> tag. mytemplate.php is now my template for all .html file. What I removed was replaced with some simple PHP instructions which load the content of the page :

if (preg_match('![/\\:]!', $_GET['page'])) return;
$k = file_get_contents($_GET['page'].'.html');
$k = preg_replace('/^.*<!-- end of menu -->/sm', '', $k);
$k = preg_replace('!^\s*<!--start of footer -->.*$!sm', '', $k);
echo $k;
The first instruction is a security measure, just in case someone tries to exploit this template with URL like /mytemplate.php?page=/etc/passwd (although the 2nd line allows only .html file to be read). The second line loads the .html file. The 3rd and 4th lines strip headers and footers, because they are now provided by the template mytemplate.php. Looking for the pattern was the hardest thing to do, but if some of the html files missed these pattern, I could add them manually. And that's it. For any change I need to do, I have to play with mytemplate.php only. The other good surprise is that I can mask all the changes I've made just by disabling the rewriting in .htaccess.