skip to Main Content

I am asking because I can not for the live of me figure out what is wrong and so far none of the StackOverflow answers worked.

I have to redirect a domain to another subdomain, except the admin. For example:

sub1.domain.com/testsite/ shoud redirect to “sub2.domain.com/testsite/”,
but sub1.domain.com/admin/ or “sub1.domain.com/de/admin/” should stay right where it is.

As a first step I tried to only check for the “admin”, so everything would be redirected except “sub1.domain.com/admin/”:

RewriteCond %{HTTP_HOST} ^sub1.domain.com
RewriteCond %{REQUEST_URI} !^/admin
RewriteRule ^/?(.*)$ http://sub2.domain.com%{REQUEST_URI} [R=301,L]

This one looked most promising, but it is not working. The second condition is not working and the admin page still gets redirected.

If anyone can help I would appreciate it.


EDIT:
I should have said that its a multi-domain site, which means we have a .htaccess file for all sites and that is the reason I specifically check for the domain.

3

Answers


  1. Chosen as BEST ANSWER

    So,

    I just found the reason (besides my stupidity). The site I should redirect was a Drupal Site. Thats why all links end up at the same location:

    sub1.domain.com/index.php

    The reason why my above Rewrite Condition was not working is, that sub1.domain.com/admin is being redirected to sub1.domain.com/index.php, which consequently ends up at: "sub2.domain.com/index.php". The correct rewrite rule looks like that:

     RewriteCond %{HTTP_HOST} ^sub1.domain.com
     RewriteCond %{REQUEST_URI} !^/(admin|index.php|de/admin|it/admin|user|de/user|it/user)
     RewriteRule (.*) http://sub2.domain.com%{REQUEST_URI} [R=301,L]
    

    This redirects everything except:

    sub1.domain.com/admin
    sub1.domain.com/de/admin
    sub1.domain.com/it/admin
    sub1.domain.com/user
    sub1.domain.com/de/user
    sub1.domain.com/it/user
    

    and of course

    sub1.domain.com/index.php
    

    Since the last one also should not be redirected if the user types it in directly, it is not a perfect solution, but I can live with it.


  2. I’m just posting this, but I can’t test it!
    But I guess this redirects EVERYTHING except that one domain.

    RewriteCond %{HTTP_HOST} !^sub1.domain.com/admin/ [NC]
    RewriteRule ^/(.*)$http://sub2.domain.com%{REQUEST_URI} [R=301,L,NC]
    

    I hope it works!

    Login or Signup to reply.
  3. RewriteCond is use to check condition weather to execute .htacess or not

    For your case the solution may be as below:-

    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^domain.com [NC]
    RewriteRule ^(admin)$ http://sub1.domain.com/$1 [R=301,L]
    RewriteRule ^(.*)$ http://sub2.domain.com/$1 [R=301,L]
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search