Question

as i am new to .htaccess, i couldn´t figure out how to make this work: On my webserver the default index.html (/var/www/) should be redirected to different other index.html regarding the time of the day and the browser language...

  • /index.html (default, german, black background for the night)
  • /i/index.html (german, white background for the day)
  • /e/index.html (english, black background for the night)
  • /e/i/index.html (english, white background for the day)

All pages are linked with each other, so that the user can jump between language and "style". Using only the time-condition it works for german, but how can i combine this with "english"? I tried this one, but it didn´t work...

Options +FollowSymLinks
RewriteEngine On
RewriteBase /


RewriteCond %{TIME_HOUR} >07
RewriteCond %{TIME_HOUR} <16
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^index\.html$ /e/i/index.html [L]

RewriteCond %{TIME_HOUR} >07
RewriteCond %{TIME_HOUR} <16
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^index\.html$ /i/index.html [L]


RewriteCond %{TIME_HOUR} >16
RewriteCond %{TIME_HOUR} <07
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^index\.html$ /e/index.html [L]

RewriteCond %{TIME_HOUR} >16
RewriteCond %{TIME_HOUR} <07
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^index\.html$ /index.html [L]

Could somebody help please? Many Thanks!

I had to change the structure of the website, so i also changed the .htaccess file:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP:Accept-Language} (de) [NC]
RewriteCond %{TIME_HOUR} >07
RewriteCond %{TIME_HOUR} <16
RewriteRule ^index\.html$ /d/i/index.html [L]
RewriteRule ^index\.html$ /d/index.html [L]



RewriteCond %{HTTP:Accept-Language} (en) [NC]
RewriteCond %{TIME_HOUR} >16
RewriteCond %{TIME_HOUR} <07
RewriteRule ^index\.html$ /e/i/index.html [L]
RewriteRule ^index\.html$ /e/index.html [L]

Now the time-condition for german works fine, but it seems that the english-condition is not even passed to the browser... Could somebody give me a hint, what could be the problem? Many thanks!

Was it helpful?

Solution

I found a solution, that works for me...

The file with the prepared times in /var/spool/timing:

RewriteCond %{TIME_HOUR}%{TIME_MIN} >0816

RewriteCond %{TIME_HOUR}%{TIME_MIN} <1603

RewriteCond %{TIME_HOUR}%{TIME_MIN} >0816

RewriteCond %{TIME_HOUR}%{TIME_MIN} <1604

RewriteCond %{TIME_HOUR}%{TIME_MIN} >0815

RewriteCond %{TIME_HOUR}%{TIME_MIN} <1605

RewriteCond %{TIME_HOUR}%{TIME_MIN} >0815

RewriteCond %{TIME_HOUR}%{TIME_MIN} <1606

...

...

...

...

And the two files with the rest of the parts, also in /var/spool/timing .begin

Options -Indexes +FollowSymLinks

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP:Accept-Language} ^.*en.*$ [NC]

RewriteRule ^index\.html$ /e/i/index.html  [L]

RewriteCond %{HTTP:Accept-Language} ^.*de.*$ [NC]

and .end

RewriteRule ^index\.html$ /d/i/index.html  [L]

RewriteRule ^index\.html$ /d/index.html  [L]

RewriteCond %{HTTP:Accept-Language} ^.*en.*$ [NC]

RewriteCond %{TIME_HOUR} >17

RewriteCond %{TIME_HOUR} <06

RewriteRule ^index\.html$ /e/index.html  [L]

The script daily takes the first two line and puts them into a new one. Then it combines all three files into the new .htaccess. In the end it cleans the files again.

#!/bin/sh

sed -n 1,2p /var/spool/timing/2014.txt >> /var/spool/timing/.mid

cat /var/spool/timing/.begin /var/spool/timing/.mid /var/spool/timing/.end > 
/var/www/.htaccess

sed -i '1,2d' /var/spool/timing/.mid & sed -i '1,2d' 
/var/spool/timing/2014.txt

so that in the end there is one new .htaccess every day:

Options -Indexes +FollowSymLinks

RewriteEngine On

RewriteBase /

RewriteCond %{HTTP:Accept-Language} ^.*en.*$ [NC]

RewriteRule ^index\.html$ /e/i/index.html  [L]

RewriteCond %{HTTP:Accept-Language} ^.*de.*$ [NC]

RewriteCond %{TIME_HOUR}%{TIME_MIN} >0816

RewriteCond %{TIME_HOUR}%{TIME_MIN} <1603

RewriteRule ^index\.html$ /d/i/index.html  [L]

RewriteRule ^index\.html$ /d/index.html  [L]

RewriteCond %{HTTP:Accept-Language} ^.*en.*$ [NC]

RewriteCond %{TIME_HOUR} >17

RewriteCond %{TIME_HOUR} <06

RewriteRule ^index\.html$ /e/index.html  [L]

It works nice so far, i just have to set up a new 2015.txt at the end of this year - if thats nacessary at all...I think i will take it every year again.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top