Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8135949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:35:19+00:00 2026-06-06T10:35:19+00:00

I want if my visitors go to subdomain.example.com that they get redirected to anothersubdomain.example.com

  • 0

I want if my visitors go to subdomain.example.com that they get redirected to anothersubdomain.example.com. And if they go to css.subdomain.example.com that they get redirected to css.anothersubdomain.example.com etc.

I’ve tried the following regexes (with preg_match):

Attempt 1:

if(preg_match('#(([\w\.-]+)\.subdomain|subdomain)\.example\.com#', $_SERVER['SERVER_NAME'], $match)) {
    header('Location: http://'.$match[1].'anothersubdomain.example.com/');
}

If they go to: subdomain.example.com they get redirected to: anothersubdomain.example.com

But If they go to: css.subdomain.example.com they get redirected also to: subdomain.example.com – So that is not working

Attempt 2:

if(preg_match('#([\w\.-]+)\.subdomain\.example\.com#', $_SERVER['SERVER_NAME'], $match)) {
    header('Location: http://'.$match[1].'.anothersubdomain.example.com/');
}

If they go to: css.subdomain.example.com they get redirected to: css.anothersubdomain.example.com

But If they go to: subdomain.example.com they get redirected to: .subdomain.example.com – And that URL is not valid so also this attempt is not working.

Somebody has an answer? I want not to use nginx or apache rewrites.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:35:20+00:00Added an answer on June 6, 2026 at 10:35 am

    This worked for me:

    $tests = array(
        'subdomain.example.com' => 'anothersubdomain.example.com',
        'css.subdomain.example.com' => 'css.anothersubdomain.example.com'
    );
    
    foreach( $tests as $test => $correct_answer) {
        $result = preg_replace( '#(\w+\.)?subdomain\.example\.com#', '$1anothersubdomain.example.com', $test);
        if( strcmp( $result, $correct_answer) === 0) echo "PASS\n";
    }
    

    What I did is made the capturing group for the “first” subdomain optional. So, if you printed out the results like so:

    foreach( $tests as $test => $correct_answer) {
            $result = preg_replace( '#(\w+\.)?subdomain\.example\.com#', '$1anothersubdomain.example.com', $test);
        echo 'Input:    ' . $test . "\n" . 
             'Expected: ' . $correct_answer . "\n" . 
             'Actual  : ' .$result . "\n\n";
    }
    

    You’d get as output:

    Input:    subdomain.example.com
    Expected: anothersubdomain.example.com
    Actual  : anothersubdomain.example.com
    
    Input:    css.subdomain.example.com
    Expected: css.anothersubdomain.example.com
    Actual  : css.anothersubdomain.example.com
    

    Now to apply it to your needs:

    if( preg_match( '#(\w+\.)?subdomain\.example\.com#', $_SERVER['SERVER_NAME'], $matches)) {
        echo header( 'Location: http://'. (isset( $matches[1]) ? $matches[1] : '') .'anothersubdomain.example.com/');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to redirect my visitors from http://www.example.com and http://example.com to
I want to redirect visitors to my main domain when they perform requests on
I want to get an ip address of visitors. could you tell me what
I want visitors to my website to fill out a contact form before they
I want to redirect visitors who get to my old phpbb forum URLs to
Let's say I have two steps of my form that I want my visitors
There is a web site www.example.com All cookies are set to the www subdomain.
I want to develop an application that allows me to track website visitors. I'm
I want to get the geographic location of my website visitors and store it
I have a website where i want my visitors to invite their friends. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.