1

I'm running a server with multiple vhosts and phpMyadmin is set up as an alias which can be access via anydomain.com/phpmyadmin. I would like to use an .htaccess redirect rule so that if phpmyadmin is NOT accessed on the server-admin-url, the visitor is redirected to, say, Google.

The correct URL would be: https://server.domain.com:9090/phpmyadmin

Could anyone help me, please?

Thanks!

zkvvoob
  • 394
  • 1
  • 4
  • 26

1 Answers1

6

The easiest way you can do this

RewriteEngine On
RewriteCond %{HTTP_HOST} !^server.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.google.com/$1 [L,R]

Maybe you want also check the port, then use

RewriteCond %{SERVER_PORT} !^9090$ [OR]
RewriteCond %{HTTP_HOST} !^server.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.google.com/$1 [L,R]

Update

Reading again your question I see you're also trying to switch from http to https.

I suggest to add a check if https is off:

RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^server.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.google.com/$1 [L,R]

If your VirtualHost is configured with https you should pay particular attention to how VirtualHost matching works

freedev
  • 25,946
  • 8
  • 108
  • 125
  • Hi, freedev. Thanks a lot. The redirect works nicely if phpMyadmin is loaded on a different host. But if I simply use server.domain.com/phpmyadmin, i.e. without the 9090 port, it still loads. My main problem is that without the port, it won't get the correct SSL certificate. What's the best solution here? – zkvvoob Aug 22 '16 at 17:33
  • I think the real question is how do I include the two conditions as "either - or". I tried putting the one with the port first and then adding `[OR]` at the end, but it wouldn't work. What am I doing wrong? – zkvvoob Aug 23 '16 at 05:46
  • @zkvvoob Had you tried to add second rule? Just like my answer, but in this case add only `RewriteCond %{SERVER_PORT} !9090` just before the `RewriteRule`. It should work fine (without using any `[OR]`) – freedev Aug 23 '16 at 07:11
  • Yes, I've done this. Like I said, the redirect works fine if the domain is wrong, but if the port is missing - it doesn't work, i.e. phpMyadmin still loads (but with the incorrect SSL certificate). – zkvvoob Aug 23 '16 at 16:03
  • This is should work, I have updated `RewriteCond` in my answer just to be more clear, anyway suggest to enable the rewrite debugging http://stackoverflow.com/questions/9632852/how-to-debug-apache-mod-rewrite – freedev Aug 23 '16 at 16:52
  • Hello. Tried your second suggestion (with the port), but when I go to `https://server.maindomain.com:9090/phpmyadmin` I'm redirected to Google! Here's the apache error log: http://pastebin.com/Z3ScuwMJ Could you help me figure out what's wrong? – zkvvoob Aug 24 '16 at 16:19
  • @zkvvoob I don't see the `RewriteCond` part in the piece of log you have posted in pastebin, there is only the `RewriteRule` part. Have you truncated the log? Or double check if there is a second `.htaccess` into `/usr/share/phpmyadmin`. Where have you placed your `.htaccess`? Could you post your `.htaccess` entirely? – freedev Aug 25 '16 at 10:27
  • Thank you for not giving up! There is no other .htaccess file in `/usr/share/phpmyadmin` where I've put mine. According to the Apache vhost, this is the directory behind the `/phpmyadmin` alias. Here's the full content of the .htaccess, there's nothing there besides your lines (note that the port IS correct): http://imgur.com/a/73kkt As for the apache error.log, I followed the instructions here http://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#logging and used `tail -f` as described on the same page to get the results I shared with you earlier. – zkvvoob Aug 25 '16 at 15:41
  • Regarding the log, may be you had a low verbosity. Anyway, I have updated my answer hope this new suggestion helps. – freedev Aug 26 '16 at 00:47