Forum Moderators: phranque
[apple.example.com...] rewrite to: http://www.example.com/apple/aaa.html
here is I write in the .htaccess:
RewriteEngine on
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{REQUEST_URL} ^([^.]+)\.example\.com/.([^.]+)
RewriteCond $1!^.
RewriteRule (.*) /%1/%2 [L]
But It Can't Work.
In .htaccess, rewrite looping must be explicitly prevented; Otherwise the output of the rewrite rule will be repeatedly rewritten, with the result in this case being /apple/apple/apple/apple/apple/.../apple/aaa.html until the server reaches its maximum internal redirection limit, or the request becomes too long and a 414 Request-URI Too Long or a 400 Bad Request error is encountered.
One method to prevent this is to use an environment variable. Initially, it will be undefined, and the RewriteCond will allow the rule to perform a rewrite. But as soon as the rewrite is performed, we define the variable so that the RewriteCond will fail on the next pass, stopping any further rewriting by this rule.
RewriteEngine on
#
RewriteRule ^\.htaccess$ - [F]
#
RewriteCond %{ENV:RewriteDone} !^Yes$
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /%1/$1 [E=RewriteDone:Yes,L]
Jim