简体   繁体   中英

How do I redirect from a folder on a subdomain to main domain

Bit of additional info...

PHPBB has a way to redirect you after login to the page you were looking at before, even if you're using the login-form externally on your own pages. However they decided it should be a relative path within your own domain. Which is not exactly great if you have the forum on a subdomain like forum.mydomain.com...

The example from their wiki : <input type="hidden" name="redirect" value="./somefile.html" />

To work around the problem of the subdomain, I figured I would add an indication of a frontpage redirect to it, like <input type="hidden" name="redirect" value="./redirect/frontpagenews.php" />

So if you use the login-form on the page http://mysite.com/frontpagenews.php the forum redirect url ends up looking like forum.mysite.com/redirect/frontpagenews.php (this works, the forum creates this link to go back to after login). All it needs is a rewrite rule to detect this so it would actually sent you back to http://mysite.com/frontpagenews.php

The question...

How do I go from forum.mysite.com/redirect/*anything* to mysite.com/*anything* ?

I've been at this for a while now, and I think the code below should work, except it doesn't. I've put it above the other forum rewrite rules in the htaccess file in forum.mysite.com as it probably should be fine if it's the first rule that's checked.

RewriteCond %{HTTP_HOST} ^forum\.mysite\.com/redirect/(.*)$ [NC]
RewriteRule (.*) http://mysite\.com/$1 [QSA,L,NC]

Also tried this as condition, but no joy:

RewriteCond %{REQUEST_URI} /redirect/(.*)$ [NC]

The HTTP_HOST only matches against a hostname, not the path. The path needs to be part of the RewriteRule. Assuming that the .htaccess file that you are editing resides in the forum.mysite.com root (as in the equivalent of: forum.mysite.com/.htaccess), it should look like this:

RewriteCond %{HTTP_HOST} ^forum\.mysite\.com$ [NC]
RewriteRule ^redirect/(.+)$ http://mysite.com/$1 [L,NC,R]

You won't need the QSA in the rule brackets because the query string will get appended anyways (unless you have a ? in the target, eg http://mysite.com/$1?p=1 ).

This will redirect the browser from forum.mysite.com/redirect/*anything* to mysite.com/*anything*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM