Is it possible to set a page title with header?

Viewed 35

I have a main page (index.php) and if the client is not logged in, it redirect to the login.php.

header("Location: login.php");

The problem is that the search engines display as name the "example.com" instead of the title of the index or the login.

I am thinking if it is possible to do something like that.

header("Title: Example");
header("Location: login.php");
2 Answers

It sounds to me as though you want the Search Engine to index (and pick up the title) of your login page only - not the redirecting one, so set an HTTP status code in your redirect to inform them of the correct status of those pages.

I'd suggest using 302 (Found) - this will indicate to the Search Engines that - while the page exists - it's redirecting them (and non-logged in users) to another page - which is the one of interest.

Something like this:

header("Location: https://www.example.com/login.php", true, 302);

Then on your login page just set up the HTML correctly as you would otherwise including:

<head><title>Your Page Title</title></head>

Now search engines will not index the redirected page, but only the login page with your correctly set title.

Related