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 562549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:33:59+00:00 2026-05-13T12:33:59+00:00

I want to redirect all www.domain.com requests to domain.com with PHP, basically: if (substr($_SERVER[‘SERVER_NAME’],

  • 0

I want to redirect all http://www.domain.com requests to domain.com with PHP, basically:

if (substr($_SERVER['SERVER_NAME'], 0, 4) === 'www.')
{
    header('Location: http://' . substr($_SERVER['SERVER_NAME'], 4)); exit();
}

However I do want to maintain the requested URL like in SO, for e.g.:

http://www.stackoverflow.com/questions/tagged/php?foo=bar

Should redirect to:

http://stackoverflow.com/questions/tagged/php?foo=bar

I don’t want to rely on .htaccess solutions, and I’m unsure which $_SERVER vars I’d have to use to make this happen. Also, preserving the HTTPS protocol would be a plus.

How should I do this?

  • 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-05-13T12:33:59+00:00Added an answer on May 13, 2026 at 12:33 pm
    $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80")
    {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } 
    else 
    {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    header('Location: '. $pageURL);
    

    Would redirect the user to the exact same page, www. intact.

    So, to get rid of the www. , we just replace one line:

    $pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    if ($_SERVER["SERVER_PORT"] != "80")
    {
        $pageURL .= substr($_SERVER['SERVER_NAME'], 4).":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } 
    else 
    {
        $pageURL .= substr($_SERVER['SERVER_NAME'], 4).$_SERVER["REQUEST_URI"];
    }
    return $pageURL;
    

    And that should work.

    By the way, this is the method that is recommended by Google, as it keeps https:// intact, along with ports and such if you do use them.


    As Gumbo pointed out, he uses $_SERVER['HTTP_HOST'] as it comes from the headers instead of the server, thus $_SERVER['SERVER_*'] is not as reliable. You could replace some$_SERVER['SERVER_NAME'] with $_SERVER['HTTP_HOST'], and it should work the same way.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to redirect all requests for index.html|php|php5 to http://www.domain.co.uk/ using mod_rewrite and I've
I want to redirect all of the requests to my website www.example.com/page.php to www.example.com/1/page.php
I'm trying to redirect all subdomain requests for domain.com to www.domain.com even when the
I want to redirect all incoming requests to my root domain, and found the
You see, I want to redirect [R] from http://www.domain.com/dir1/ to http://www.domain.com/ . I also
I want to redirect all requests from ex-ample.com to example.com. The url structure should
I want to redirect one specific url like: www.example.com/test/ex.gif to static.example.com/ex.gif How can i
I want to redirect all to one script e.g. index.php?url=inputurl With if/else I want
I have the following page: www.domain.com/index.php?route=information/contact and I'd like to rewrite it so that
I want to redirect all user page requests to a page on the same

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.