0

i'am trying to do a simple redirect to a maintenance page, but seems like apache won't let me do !

i have a folder for 3 version of a wiki (each with a specific language), so i want to redirect each wiki trafic to his own maintenance page.

here is my .htaccess

 #Redirect trafic to maintenance page
 RewriteEngine On
 RewriteCond %{REQUEST_URI} !maintenance.htm$ [NC]
 RewriteRule "(.*\/wiki.*\/).*" "$1/maintenance.htm" [R=302,L]

the goal is to redirect "myserver/wiki/ANYTHING" to "myserver/wiki/maintenance.htm"

if someone could tell me what i am doing wrong, it would be nice

Thanks

Edit:

After some more test, seems like i am looking for something like that

RewriteRule "(.*)" "??PROTOCOL??://%{HTTP_HOST}/??SITEFOLD??/maintenance.htm" [R=302,L]
Froggiz
  • 3,043
  • 1
  • 19
  • 30

1 Answers1

1

This seems to work:

#Redirect trafic to maintenance page
RewriteEngine On
RewriteCond %{REQUEST_URI} !maintenance.htm$ [NC]
RewriteRule "(.*\/wiki)\/.*" "$1/maintenance.htm" [R=302,L]

Your version allows the / to be matched by the .* as well (as Apache is greedy regexpr) and also including the / in the bracket means it's repeated. So under your example:

myserver/wiki/test1/test2

would redirect to this (with test1 and double /):

myserver/wiki/test1//maintenance.htm

whereas under my example this will redirect to:

myserver/wiki/maintenance.htm

Which I think is what you want.

However, even though my solution above should work, sometimes it might be better to just simplify this for your own sanity and just have three of them. While it is a bit of repetition and is not scalable, if you are limited to three then sometimes clarity is better than cleverness. For example if you have an English wiki, a Spanish Wiki and a German wiki at /en/wiki, /es/wiki and /de/wiki respectively, then the following might just be easier.

#Redirect trafic to maintenance page
RewriteEngine On
RewriteCond %{REQUEST_URI} !maintenance.htm$ [NC]
RewriteRule "/en/wiki/.*" "/en/wiki/maintenance.htm" [R=302,L]
RewriteCond %{REQUEST_URI} !maintenance.htm$ [NC]
RewriteRule "/es/wiki/.*" "/es/wiki/maintenance.htm" [R=302,L]
RewriteCond %{REQUEST_URI} !maintenance.htm$ [NC]
RewriteRule "/de/wiki/.*" "/de/wiki/maintenance.htm" [R=302,L]
Barry Pollard
  • 4,591
  • 15
  • 26
  • that s exactly what i need, i tried, and it won't work on my server, i found something really strange: each time i set $1 the redirect won't works ... i am trying to get rewrite log, but it changed in apache 2.4 so i have to search about it too xd – Froggiz Sep 23 '15 at 11:33
  • So does the second more explicit option work? – Barry Pollard Sep 23 '15 at 11:38
  • yes it is the $1 who wont work, even RewriteBase "/wiki/" won't work – Froggiz Sep 23 '15 at 11:39
  • there was two in conflict, so now all is working thanks ! – Froggiz Oct 09 '15 at 14:49