Forum Moderators: phranque

Message Too Old, No Replies

I don't know what's wrong with the .htaccess

I don't know what's wrong with the .htaccess

         

chinaui

4:57 am on Dec 26, 2006 (gmt 0)

10+ Year Member



Here I want to do this :

[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.

jdMorgan

6:26 am on Dec 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The REQUEST_URI variable contains only the local URL-path, not the domain name.

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]

You may change the name of the RewriteDone variable if you like. I named it to describe its function, but the name is arbitrary; You can use any name except for the pre-defined server variable names.

Jim