0

I'm trying to do a Rewrite subdomains to subdirectories on htaccess. Searching here I did the rule below:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com\.br
RewriteCond %{REQUEST_URI} !^/sd_
RewriteRule ^(.*)$ http://domain.com.br/sd_%1/$1 [P,L,NC,QSA]

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)\.domain\.com\.br
RewriteCond %{REQUEST_URI} !^/sd_
RewriteRule ^(.*)$ http://domain.com.br/sd_%1/$1 [P,L,NC,QSA]

#Rewrite for my Framework
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1

When I access the address: http://user.domain.com.br or the address: http://www.user.domain.com.br they're redirected without change the URL address to: http://www.domain.com.br/sd_user/

Well, fine. My problem is:
When someone access a adrees with haven't a folder, .htaccess redirect it to a specific folder. For example:
I haven't a folder called "sd_joecompany", then I want to redirect it to a default folder named "sd_system". If I access http://joecompany.domain.com.br/ and not exists the respective folder, this address may point to "sd_system" keeping the address.

With this code, when I access a subdomain without a respective folder, the following error appear:

Proxy Error

The proxy server received an invalid response from an upstream server. The proxy server could not handle the request GET /index.php.

Reason: Max-Forwards has reached zero - proxy loop?

I'm not sure about the clarity of my doubt, and I'm sorry for my poor english. I know a little about .htaccess and these rules. Everithing on the code above, I gathered here!

Thank you since now!

  • Is the `user.domain.com.br` and `www.domain.com.br` on the same server and use the same document root? – Jon Lin Sep 06 '13 at 17:53
  • Yes! On the same server and the same root. I've added on the server a DNS rule to redirect all trafic of (asterisk).domain.com.br and www.(asterisk).domain.com.br to the IP of domain.com.br – Káliman Borges Sep 06 '13 at 18:16
  • Then, without the rules, all traffic of subdomains are redirect to the root of domain.com.br – Káliman Borges Sep 06 '13 at 18:18

1 Answers1

2

If they are on the same server and have the same document root, then you really don't want to proxy at all. Remove the host part of the rule and get rid of the "P":

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com\.br$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sd_%1/$1 [L,NC,QSA]

RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com\.br$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sd_%1/$1 [L,NC,QSA]

#Rewrite for my Framework
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1

Cleaned up some of the regex and got rid of the extra RewriteEngine On's.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Didn't work for subdomains without directories for they. "Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request." For the default access and for the subdomains with the folder (sd_subdomain) it's ok. – Káliman Borges Sep 06 '13 at 19:46
  • @KálimanBorges when I put those rules in a blank htaccess, I can access `http://user.domain.com.br/` just fine (brings up `/sd_user/index.php`). – Jon Lin Sep 06 '13 at 20:20
  • Because exists the folder "sd_user" on your root, right? If you try access `http://otheruser.domain.com.br/` the error will appear. – Káliman Borges Sep 06 '13 at 22:41
  • @KálimanBorges If the file isn't there, then what's supposed to be served? Is it through the framework? – Jon Lin Sep 06 '13 at 23:20
  • If the file(folder) isn't there, would be necessary a redirect to other place (root, for example). With error the framework can't do anything. I don't know how, but I think in something like a conditional. If the folder exists, continue the execution of the rule, else, redirect to a specific folder or address. – Káliman Borges Sep 06 '13 at 23:54
  • @KálimanBorges I don't get any errors even if the file isn't there, no matter what I put in as the subdomain. But, if you want to check if the file exists before rewriting, simply add the condition – Jon Lin Sep 07 '13 at 00:03
  • after I added `RewriteCond %{SCRIPT_FILENAME} -d` to the begining of each rule the redirection was ignored with no error. No matting what I put on subdomain. Right! But I want redirect subdomains without directory to a directory, for example, to `sd_sistema`. Then I added `RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /sd_sistema/` but this rule only work if I remove the rule that makes possible my framework (the last one). Do you know how I can do both work? I'm very gratefull with your help! – Káliman Borges Sep 07 '13 at 04:40