开发者

Subdomain to Folder using Apache mod_rewrite related Problem

开发者 https://www.devze.com 2023-03-27 11:07 出处:网络
I want to map all my subdomain to folder. For example subdomain.domain.com should display content of domain.com/clients/subdmain (Transparently)

I want to map all my subdomain to folder.

For example subdomain.domain.com should display content of domain.com/clients/subdmain (Transparently)

RewriteCond %{HTTP_HOST} ^(.+)\.oomail\.org
RewriteCond %{HTTP_HOST} !^www\.oomail\.org [NC]
RewriteRule ^(.+)$ /var/www/clients/%1/$1

I got 500 Internal Server Error. To check whats the problem I appended [R] flag

RewriteRule ^(.+)$ /var/www/clients/%1/$1 [R]

I got this result in address bar

http://q1.oomail.org/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clie开发者_开发问答nts/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/var/www/clients/q1/always.html

Then I created a details.php file which dumps the $_GET variable details. Its content is

<pre>
<?php
var_dump($_GET);
?>
</pre>

and modified RewriteRule to (Note: I am sending $1 and %1 into details.php which shows its content)

RewriteRule ^(.+)$ details.php?p=%1&d=$1

And after opening http://subdomain.oomail.org/file.html, I got output as

array(2) {
  ["p"]=>
  string(9) "subdomain"
  ["d"]=>
  string(11) "details.php"
}

Of which variable p is correct (value of %1) which is "subdomain", BUT d (value of $1) is details.php which I was expecting to be file.html which I opened in URL. Where am I going wrong? or How can I fetch file.html (URI) into variable for mapping?


Use this rule:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/clients/
RewriteCond %{HTTP_HOST} ^(.+)\.oomail\.org
RewriteCond %{HTTP_HOST} !^www\.oomail\.org
RewriteRule ^(.*)$ /clients/%1/$1 [L]

In your first attempt you had endless rewrite loop which Apache had to interrupt (by default no more than 10 iterations). In second attempt you are facing limited rewrite loop (2 iterations, I believe). That's how mod_rewrite works and you should count such possibility in your rules -- especially when you use .* as matching pattern .

The very first condition in my rule will prevent rewrite loop from happening (means rule will only work on very first iteration). Unfortunately this means some restrictions: the original URL CANNOT start with /clients/. For example: this URL will not be redirected: http://something.example.com/clients/hello-kitten.php.

Solution -- rename clients folder to something more unique: for example _clients_folder_ (you would have to change that folder in rewrite rules as well, obviously).

Something to read: RewriteRule Last [L] flag not working?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号