Forum Moderators: phranque

Message Too Old, No Replies

Redirect Subdomain to directory with parameter

Problem with parameter

         

abm24

5:12 pm on Sep 30, 2009 (gmt 0)

10+ Year Member



Hi,

I want to redirect a subdomain to a real directory on my server.

Here's what I got after searching this forum:


RewriteCond %{HTTP_HOST} ^sub\.mydomain\.co\.uk [NC]
RewriteRule ^(.*) /home/user/domains/mydomain.co.uk/public_html/sub/$1 [NC,L]

The problem is the '$1' part. When I remove this, or replace it with a file on the directory - it works like a charm.
But if I leave it there (and I need it, becuase I want the parameter) then I get HTTP 500 Error.

What am I doing wrong? :-(

Thanks!

jdMorgan

6:56 pm on Sep 30, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is to stop this rule from looping, rewriting (not "redirecting") that /sub path to itself over and over again and causing the 500-Server Error that you'll likely find detailed in your server error log?

Also, the length of your substitution filepath indicates that your site's DocumentRoot is likely not configured properly (or at all).

The code, assuming that you fix that DocumentRoot problem will likely look more like:


# Externally redirect to force subdomain canonicalisation
RewriteCond %{HTTP_HOST} ^sub\.example\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !=sub.example.co.uk
RewriteRule ^(.*)$ http://sub.example.co.uk/$1 [R=301,L]
#
# Internally rewrite requests for "sub" subdomain to "/sub" folder
RewriteCond %{HTTP_HOST} ^sub\.example\.co\.uk
RewriteCond $1 !sub/
RewriteRule ^(.*)$ /sub/$1 [NC,L]

Note that the new RewriteCond in the second rule prevents recursion. If you intend to add more subdomains at any time in the future, then I suggest you change your directory structure to put *all* subdomains into a 'subdomains' subdirectory from the outset. Otherwise, you will have to add new rules every time you add a subdomain, because each will require a one-off solution to preventing recursion.

e.g. map the URL <the-subdomain-name>.example.co.uk/foo to the filepath /subdomains/<the-subdomain-name>/foo so that all you need test for is the "/subdomains/" path already being present in order to stop a loop, and that same test will work for any subdomain.

Jim

[edited by: jdMorgan at 10:07 pm (utc) on Sep. 30, 2009]

abm24

9:09 pm on Sep 30, 2009 (gmt 0)

10+ Year Member



Wow Jim, I'm amazed.
Thanks a million.