views:

338

answers:

5

I'm programming with PHP.

I want to redirect https://abc.example.com/blah1/blah2 to https://www.example.com/blah1/blah2 but I don't want to redirect https://abc.example.com redirect to https://www.example.com

Is it possible while preserving session information across the redirections?

Thank you.

A: 

Use a javascript redirect.

Janie
What happens when a user has Javascript disabled?
MitMaro
+1  A: 

If you’re using a cookie for the session ID, then you need to set the cookie for a common domain. If you are using www.example.com and foobar.example.com, you need to set the cookie for example.com to have it valid for both www.example.com and foobar.example.com.

Gumbo
A: 

If you can use apache's Redirect you can try

RedirectMatch /(.+) https://www.domain.com/$1

with PHP it would be

<?php


    if ($_SERVER['REQUEST_URI'] != "/") {
        header("Location: ".$_SERVER['REQUEST_URI']);
        exit;
    }

?>

About session being invalidated, like Gumbo says, have the cookie issued for the main domain name instead of the specific ones.

Vinko Vrsalovic
`REQUEST_URI` is *never* empty. It always starts with at least `/`.
Gumbo
Are you certain of that? I edited accordingly.
Vinko Vrsalovic
And also it seems I misunderstood the question.
Vinko Vrsalovic
+3  A: 

You can continue using the redirects as you have them now, but adjust your session.cookie_domain to use the top-level domain (e.g. example.com). You can do this by using session_set_cookie_params or setting session.cookie_domain in your php.ini file (or in a .htaccess file after php_value directive). That should allow your session information to persist across all sub-domains of your site.

tj111
what if I just use session without cookie?
Yc Zhang
Sessions in PHP by default use cookies to store the *session id*. Then when the visitor loads a page, PHP uses that ID to fetch the data associated with that session. So when you create a session, you are also creating a cookie, although the only data it contains is the session id. It is possible to use sessions without a cookie, however it passes the session id around as a query parameter which has some serious security flaws.
tj111
A: 

I'd say you can redirect by posting (POST) your sessionid to a new domain and store your session data in mysql with your own session handler.