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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:46:02+00:00 2026-06-12T19:46:02+00:00

Because header(Location: ); header needs to be used with absolute paths rather then relative

  • 0

Because header("Location: "); header needs to be used with absolute paths rather then relative ones, I made up this function to get a absolute path by using the $_SERVER variables.

function getAbsolutePath($relativePath = "/site/123/") {
if (isset($_SERVER['HTTPS'])) {
    $protocol = 'https'; 
} else {
    $protocol = 'http';
}

$host = $_SERVER['HTTP_HOST'];

$port = $_SERVER['SERVER_PORT'];

$absolutePath = 'Location: ' . $protocol . "://" . $host  . ":" . $port . $relativepath;
return $absolutePath;
}

Is this the perfect method to do so, or are they any better alternatives?

  • 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-12T19:46:03+00:00Added an answer on June 12, 2026 at 7:46 pm

    That’s the “textbook” way of making an absolute URI.

    A few caveats I would like to highlight.

    1. $_SERVER['HTTPS'] may not always be defined; the condition of $_SERVER['SERVER_PORT'] == 443 should be inspected in that case; if you’re behind a HTTPS load balancer that forwards the traffic via HTTP and none of the aforementioned methods work, you may have to hard code it.

    2. $_SERVER['HTTP_HOST'] is not always defined either; this happens rarely, HTTP/0.9 didn’t define it and some home brew HTTP scripts may not pass it. You could look at $_SERVER['SERVER_NAME'] to find an alternative.

    3. If the protocol and port is http, 80 or https, 443 respectively, you don’t need to add :$port.

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

Sidebar

Related Questions

I've found this code $(window).scroll(function(){ $('#header').css({ //Why this 15, because in the CSS, we
Becase I've seen (and used) situations like this: In header.h: class point { public:
because when I use the function getUser () do not get the correct id?
Because I've made .cpp files and then transferred them into .h files, the only
<? echo lalala; header(Location: http://www.google.com/); If i put this in a plain php file
Because data from file look like this: line 1 is name (first last), next
Because of min time betwwen update it display the location update after 1 sec
I use a lot of header redirects but because of that I miss some
How can I get session variables to persist across header redirects? My header redirect
This code really made me confused. The first and second time I ran it,

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.