Forum Moderators: phranque

Message Too Old, No Replies

Check if file exist

Problems

         

johnnyfilipov

9:32 pm on Aug 22, 2007 (gmt 0)

10+ Year Member



RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.net$ [NC]
RewriteCond %{REQUEST_URI}!^/mydir/([^.]+)(/.*)?$ [NC]
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* [domain.net...] [R=301,L]

it doesn't work :(.

jdMorgan

11:43 pm on Aug 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How does this "not work"?

  • What do you intend it to do?
  • What URLs did you test with?
  • What results did you expect?
  • What were the actual results?
  • How did the actual results differ from the expected results in terms of you intent?

    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

  • johnnyfilipov

    8:13 am on Aug 23, 2007 (gmt 0)

    10+ Year Member



    this is my new code
    Options +FollowSymLinks

    RewriteEngine 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>

    jdMorgan

    12:42 pm on Aug 23, 2007 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    In you code, you have both "-d", meaning "exists as a directory" and "!-d", meaning "does NOT exist as a directory." Are these all correct for the condition you wish to test?

    Jim

    johnnyfilipov

    2:43 pm on Aug 23, 2007 (gmt 0)

    10+ Year Member



    Yes.

    When i try with real folder, it works, but i don't have real folders, i have rewrited ones.

    Like this

    .htaccess file in /mydir/
    <Files xx>
    ForceType application/x-httpd-php
    </Files>

    in root directory is my major htaccess file.

    jdMorgan

    3:41 pm on Aug 23, 2007 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    OK, I think I understand what you are trying to do. So, I would suggest:

    # 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]

    Use THE_REQUEST in the first rule to prevent your first and second rule from interfering with each other, and creating an "infinite loop." THE_REQUEST is the HTTP request header received from the client. It is NOT affected by internal rewrites. It looks something like this:
    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

    johnnyfilipov

    4:58 pm on Aug 23, 2007 (gmt 0)

    10+ Year Member



    It works like a dream, tnx Jim.

    at least somebody is trying to understand my words, thank you ;)
    my english is worse, i know this :(

    i have one little problem
    when i type asd.zxc.domain.net or asd.zxc.zxczxc.zczxczx.zxc.domain.net etc -> no redirect, how can fix this issue?

    jdMorgan

    5:21 pm on Aug 23, 2007 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    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

    johnnyfilipov

    5:25 pm on Aug 23, 2007 (gmt 0)

    10+ Year Member



    I do not know what you would want to do if you received a request for those sub-subdomains or sub-sub-subdomains...

    if client request url with more than one sub.domain.net -> redirect to domain.net.

    tnx

    jdMorgan

    8:28 pm on Aug 23, 2007 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Add a new rule before the second rule shown above:

    # 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

    This will only work if your DNS is set up to deliver these requests to your server. Otherwise, the client will get a "Server not found" DNS error.

    Jim