Forum Moderators: phranque
it doesn't work :(.
There is one line that appears to not be needed, and another that appears to have an error, but without knowing anything about what you are trying to accomplish, it would not be a good use of time to discuss these issues.
If you give us some information, we may be able to help...
Jim
Options +FollowSymLinksRewriteEngine On
# Redirect requests for /mydir/xx/ to xx.domain.net(doesn't work; no redirect)
RewriteCond %{HTTP_HOST} ^domain\.net$ [NC]
RewriteCond %{REQUEST_URI} ^(/mydir/([^/]+))(/(.*))?$ [NC]
RewriteCond %{DOCUMENT_ROOT}%1/ -d
RewriteRule .* http://%2.domain.net/%4 [R=301,L]
# Rewrite xx.domain.net to /mydir/xx/ (doesn't work; result is redirecting every time to domain.net)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.net$ [NC]
RewriteCond %{DOCUMENT_ROOT}/mydir/%1/ -d
RewriteRule ^(.*)$ /mydir/%1/?req_uri=$1 [QSA,L]
# If it's xx.domain.net and /mydir/xx does not exists,
# redirect to the main domain.(doesn't work)
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.net$ [NC]
RewriteCond %{DOCUMENT_ROOT}/mydir/%1/!-d
RewriteRule .* http://domain.net/? [R=301,L]
i don't have directoies i have
<Files xx>
ForceType application/x-httpd-php
</Files>
# Redirect [b]only[/b] direct [i]client[/i] requests for /mydir/xx/yy to xx.example.net/yy
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /mydir/[^/]+
RewriteRule ^mydir/([^/]+)(/(.*))?$ http://$1.example.net/$3 [R=301,L]
#
# [b]If[/b] /mydir/yy exists, internally rewrite xx.example.net/yy to /mydir/xx/?req_uri=yy
RewriteCond %{HTTP_HOST} !^www\.example\.net [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.net [NC]
RewriteCond $1 !^mydir/
RewriteCond %{DOCUMENT_ROOT}/mydir/%1 -d
RewriteRule (.*) /mydir/%1/?req_uri=$1 [QSA,L]
#
# If xx.example.net requested and /mydir/xx does not exist, redirect to the main domain.
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.net [NC]
RewriteCond %{DOCUMENT_ROOT}/mydir/%1 !-d
RewriteRule .* http://example.net/? [R=301,L]
#
# Externally redirect www.example.net/xyz to example.net/xyz
RewriteCond %{HTTP_HOST} ^www\.example\.net
RewriteRule (.*) http://example.net/$1 [R=301,L]
GET /mydir/abc/xyz?def HTTP/1.1
Note that "-d" checks DO NOT use the trailing slash.
HTTP_HOST may have a port number appended. Example: xyz.example.com:80/foo
So, do not end-anchor hostnames, or use ^([^\.]+)\.example\.net(:[0-9]{1,5})?$
Don't use the Redirect_Status variable. It is NOT a system variable, and is not available in all contexts. I believe it is only set by PHP, so it won't usually work in .htaccess.
Not that "file exists" checks are always done last (when possible) to prevent unnecessary (and CPU-expensive) work.
If you still have trouble, please post as much info as possible from the list of questions above. It looks like maybe you are not comfortable with English, but you are fairly comfortable with mod_rewrite. So that can be the common language. :)
Jim
When i type asd.zxc.domain.net or asd.zxc.zxczxc.zczxczx.zxc.domain.net etc -> no redirect, how can fix this issue?
I don't know, because I do not know the meaning of asd.zxc.zxczxc.example.net for your site. I do not know what you would want to do if you received a request for those sub-subdomains or sub-sub-subdomains...
P.S. You speak my language much better than I speak yours!
Jim
# Redirect only direct client requests for /mydir/xx/yy to xx.example.net/yy
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /mydir/[^/]+
RewriteRule ^mydir/([^/]+)(/(.*))?$ http://$1.example.net/$3 [R=301,L]
#
[b]# If more than one subdirectory level requested, redirect to the main domain.
RewriteCond %{HTTP_HOST} ^([^.]+\.){2,}example\.net [NC]
RewriteRule .* http://example.net/? [R=301,L][/b]
#
# If /mydir/yy exists, internally rewrite xx.example.net/yy to /mydir/xx/?req_uri=yy
RewriteCond %{HTTP_HOST} !^www\.example\.net [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.net [NC]
RewriteCond $1 !^mydir/
RewriteCond %{DOCUMENT_ROOT}/mydir/%1 -d
RewriteRule (.*) /mydir/%1/?req_uri=$1 [QSA,L]
#
# If xx.example.net requested and /mydir/xx does not exist, redirect to the main domain.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.net [NC]
RewriteCond %{DOCUMENT_ROOT}/mydir/%1 !-d
RewriteRule .* http://example.net/? [R=301,L]
#
# Externally redirect www.example.net/xyz to example.net/xyz
RewriteCond %{HTTP_HOST} ^www\.example\.net
Jim