Forum Moderators: phranque

Message Too Old, No Replies

Subdomain to file rewrite

         

dpaladin

4:02 am on Jul 12, 2009 (gmt 0)

10+ Year Member



Hi

i want to do redirects from a specific subdomain to a existing file like this:

RewriteCond %{HTTP_HOST} ^panel\.domain\.com [NC]
RewriteRule ^folder(/?)$ file1.php [L,QSA]

RewriteCond %{HTTP_HOST} ^panel\.domain\.com [NC]
RewriteRule ^(.*)$ file2.php?x=$1 [L,QSA]

then if i access: [panel.domain.com...]
it must redirect to [domain.com...]

[panel.domain.com...] must go to [domain.com...]

i don't want external redirect
so: RewriteRule ^folder(/?)$ [domain.com...] [L,QSA]
doesn't work

and

RewriteRule ^folder(/?)$ [domain.com...] [P,L,QSA]

redirect with internal proxy works, and doesn't do any external redirects, but because of the use of a proxy, i can't use php sessions
$_SERVER['REMOTE_ADDR'] will be equal to server's IP and not user's IP

is there a way to redirect that panel.domain.com to domain.com/file1.php without external redirects nor internal proxy?

thank you in advance

jdMorgan

5:44 pm on Jul 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If this code is in .htaccess, then you will need to explicitly prevent an 'infinite' rewrite loop, as well as preventing the second rule from overriding the first:

RewriteCond %{HTTP_HOST} ^panel\.domain\.com [NC]
RewriteRule ^folder/?$ file1.php [L]
#
RewriteCond %{HTTP_HOST} ^panel\.domain\.com [NC]
RewriteCond $1 !^(file1¦file2)\.php$
RewriteRule ^(.*)$ file2.php?x=$1 [QSA,L]

Important: Replace the broken pipe "¦" character in the new RewriteCond with a solid pipe before use; Posting on this forum modifies the pipe characters.

There is no need for the [QSA] (Query String Append) flag on the first rule, since this rule does not add to or change anything in the query string. The default mod_rewrite behaviour in this case is to pass the original query string unchanged.

For your "folder" URLs, you should pick one format or the other -- Either require a trailing slash, or require no trailing slash. Then externally redirect the 'wrong' form to the canonical form. This will prevent problems with duplicate content in search rankings, and encourage canonical inbound links to your pages.

Any external redirects should precede all internal rewrites in your code to avoid exposing internal script paths as URLs to clients.

Jim

dpaladin

8:01 am on Jul 15, 2009 (gmt 0)

10+ Year Member



woow thanks

that worked very well =)

the QSA flag was there only for testing =P

but there's another problem, maybe is not htaccess related

well, that subdomain panel.domain.com was only for logged in users, but with the rewriterule the session is empty in both file1 and file2.php

when accessing directly the files: [domain.com...] the session is there, but not in the subdomain form
do you know why? maybe the sessions are available only in the domain/subdomain where they were created

anyway, thanks for your help, the redirection works fine =D

jdMorgan

2:37 pm on Jul 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Possibly true. If this is a persistent problem after you close all sessions and completely flush (delete) your browser cache and "start clean," look at the script closely and be sure that when it needs to refer to a URL, it refers to a variable containing a URL, and that when it needs to refer to a filepath, it's looking at a variable that contains a filepath.

URLs and filepaths are very different things, and this is especially evident when using mod_alias or mod_rewrite. Unfortunately, it is a very common error to get these two things mixed up, both in thinking and in coding.

Jim