Forum Moderators: phranque

Message Too Old, No Replies

Conditional rewrite with REMOTE ADDR and "folders"

         

perotto

8:56 am on Mar 17, 2010 (gmt 0)

10+ Year Member



Hi all!

I am a newbie in apache rewriting so i need some help from you experts on the correct syntax . I have tried to write a psaudocode which hopefully explains what i want:
If REMOTE_ADDR in 10.47.182.* AND
( we try to access ”/” (root) OR ”/folder/” ) then
Rewrite to [www***.mysite.com...]

If REMOTE_ADDR NOT in 10.47.182.* AND
( we try to access ”/” (root) OR ”/folder/” ) then
Rewrite to [www***.mysite.int...]


Anyone that can help me ?

g1smd

10:39 am on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



First off, do you mean rewrite or redirect?

They are two different things. When rewriting the target is a filepath inside the server, not a URL.

perotto

11:06 am on Mar 17, 2010 (gmt 0)

10+ Year Member



I want the browser to point to the correct URL, so i guess i want to redirect.

perotto

12:19 pm on Mar 17, 2010 (gmt 0)

10+ Year Member



But then again - i read in [yourhtmlsource.com ] that we can use RewriteRule with a option [R] to get a redirect aswell so i think i will need the rewrite engine with the [R] option.

Anyone capable of helping me?

jdMorgan

7:07 pm on Mar 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post your best attempt at coding a solution as a basis for discussion.

If this does not seem clear, please take a look at our Apache Forum Charter [webmasterworld.com] to learn how to get the most from this forum.

Thanks,
Jim

perotto

9:20 am on Mar 18, 2010 (gmt 0)

10+ Year Member



My first try:

<IfModule mod_rewrite.c>
RewriteEngine On

#redirect external clients:
RewriteCond %{REMOTE_ADDR} ^(10\.47\.182\.*)$ # Check if the client is comming from dmz = external
RewriteRule ^/$ [myserver.com...] [R] #Redirect to "file" if accessing Document ROOT
RewriteRule ^/myfolder/$ [myserver.com...] [R] #Redirect to "file" if accessing /myfolder/



# Redirect internal clients:
RewriteCond %{REMOTE_ADDR} !^(10\.47\.182\.*)$ # Check if the client is NOT comming from dmz = Internal client
RewriteRule ^/$ http ://www.myserver.int/folder/file [R] #Redirect to "file" if accessing Document ROOT
RewriteRule ^/myfolder/$ http ://www.myserver.int/folder/file [R] #Redirect to "file" if accessing /myfolder/

</IfModule>

The reason why i need to specify the complete URL in the redirect is becouce external clients access through https and myserver.com but internals through http and myserver.int. The https and .com are always ssl offloaded and redirected to myserver.int so that every user comes to my server as http and myserver.int. But for a external redirect to work i need to specify Fully Qualified Domain name i guess.
Please Comment!

g1smd

3:29 pm on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is this code going in httpd.conf or in the .htaccess file?

There are important differences in what you need to do, based on that answer. This code looks like it is intended for httpd.conf.

Don't add comments on the same line as code. Post the comment to the line above the code it refers to.

The redirect is a 302 redirect. Is that what you wanted?

Once redirected, the user will see the new URL in their browser address bar. What is to stop them going straight to that URL for all future requests, never requesting / or /folder/ again?

If / and /folder/ are the URLs the user should continue to see and use, I would redirect users to the correct protocol depending on the IP address, and use an internal rewrite to connect the URL request to the internal script that will deliver the content.

jdMorgan

3:36 pm on Mar 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Two points:

  • RewriteConds *only* apply to the *single* RewriteRule that follows.
  • You should consider using a reverse-proxy through-put (RewriteRule [P] flag) rather than an external client redirect ([R=30x] flag) to "connect" to your back-end server.

    Also note that you can use the power of regular expressions to eliminate half of the rules above. Simply make "myfolder/" optional in the patterns for the external and internal rules by enclosing it in parentheses and following it with a "?".

    [added] Comments are not allowed on the same line as directives. Move your comments above each directive to avoid your server throwing a CPU-time-and-disk-space-wasting "warning" on each and every line... [/added]

    Jim
  • perotto

    8:04 am on Mar 19, 2010 (gmt 0)

    10+ Year Member



    Something like :
    <IfModule mod_rewrite.c>
    RewriteEngine On

    #redirect external clients:
    # Check if the client is comming from dmz = external
    RewriteCond %{REMOTE_ADDR} ^(10\.47\.182\.\d{1,3})$
    #Redirect to "file" if accessing Document ROOT or /folder/ directly
    RewriteRule ^/(folder(/?))?$ [myserver.com...] [R]

    # Redirect internal clients:
    # Check if the client is NOT comming from dmz = Internal client
    RewriteCond %{REMOTE_ADDR} !^(10\.47\.182\.\d{1,3})$
    #Redirect to "file" if accessing Document ROOT or /folder/ directly
    RewriteRule ^/(folder(/?))?$ http ://www.myserver.int/folder/file [R]

    </IfModule>

    Is it allowed with the "!" in my last RewriteCond ?
    (Trying to be a NOT operator) (inverting)
    Basically ia want this to be true if my IP is not starting with "10.47.182"

    I do want the URL to be changed in the browser. No problem letting users go to /folder/file, thats what i want them to do. I do not want them to go to "/" or "/folder/" becouse then my app will not work.

    g1smd

    7:11 pm on Mar 19, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    example.com/ and example.com/folder/ are URLs.

    Your script is in a folder as /folder/file.

    The purpose of a server is to connect URL requests to the resource inside the server that will deliver the content.

    There is no reason why the user could not be told to use / as the URL, and the a rewrite would connect that request to the script elsewhere inside the server.

    perotto

    8:14 pm on Mar 19, 2010 (gmt 0)

    10+ Year Member



    Ok, maybe i need to elaborate more on what i want.

    I DO want my users to use "/", but when they do so i want them to be redirected to "/folder/file".
    And if someone write "/folder" or "/folder/" - which there are a change for since we have sent out some information that wrongly tells them to use that URL - i also want them to be redirectet to /folder/file.

    Understand ?

    Please try to help me further reg. my second try above and my questions above.

    Thanks!

    g1smd

    10:05 pm on Mar 19, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



    It's bad form for the root URL to redirect elsewhere. The root URL should serve the content.

    I would redirect external requests for (www.)example.com/folder/ and (www.)example.com/folder/file to www.example.com/ and set up a rewrite so that requests for www.example.com/ are rewritten to the server path at /folder/file.

    This way, there is one URL for the content, and non-canonical URLs are redirected.

    perotto

    8:23 am on Mar 20, 2010 (gmt 0)

    10+ Year Member



    The point is that the root is actually another application that i DO NOT want access to since it is a admin page. Don't ask me why this is like this.

    My site is on /folder/ but it only works correctly if they access /folder/file (don't ask me why, becouse i havent created the application/site).

    Could i please ask that we focus on what i really ask about and that is how to accomplish a redirect from root and /folder/ to /folder/file ?

    Please se my post above for details.

    Thanks a million!

    jdMorgan

    10:31 pm on Mar 20, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    The site that you describe will likely never rank well in search, and the "site root" being an admin page will confuse both visitors and search engines until the design is changed or until these problems cause the site's business to fail.

    That is why the 'suggestion' was posted above to *not do this.* There is basically no reason that is good enough to spend time developing a site that is doomed to fail, which is why your original question was not answered. Now you can ignore this advice, but you've received it from at least two people who "fix Web sites" all day long...

    I would strongly suggest that you *rewrite* user requests if necessary, and *redirect* only admin requests, so that users and search engines are never redirected away from the "/" root URL. Using internal rewrites, the file locations are always rrelevant, it is the URLs that I'm talking about leaving at root here.

    Other than the potentially-serious problems already mentioned, I don't see any other trouble with the code you have posted. What are you having a problem with, specifically?

    Jim

    perotto

    6:42 am on Mar 21, 2010 (gmt 0)

    10+ Year Member



    This is not a site that i want crawled by Google or others.
    This is a Agile PLM Application created by Agile/Oracle that only will be accessed by R&D partners externally through https/ssl and local users internally all through personal authentication.

    I thank you all for your good advices that are propably good advices in generall, and surely valid in a general site available on internatt, but in my PLM world i really need advices on what i am trying to achive. If that is to "wrong" for you guys than i guess i will never get any advice that will help me.

    I hoped that there was someone out there that had any experience with rewriting and redirecting that could help me check the technical quality in my code so that there are no fitfalls in what i am doing that could compromize the generall functionallity of my application.

    Thje only techincallity i not am unsure of is this:
    Is it allowed with the "!" in my last RewriteCond ?
    (Trying to be a NOT operator) (inverting)
    Basically ia want this to be true if my IP is not starting with "10.47.182"


    Thanks!

    jdMorgan

    2:00 pm on Mar 21, 2010 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    As stated above, I see no problem with your code as most-recently posted. And as described in the mod_rewrite documentation, mod_rewrite treats a "!" character prepended to a regular-expressions pattern as a logical NOT operator. There are thousands of examples of this syntax in use in previous threads in this forum, and your usage is correct.

    Be aware that in addition to the mod_rewrite and regex syntax being correct, the code's proper function also depends on location. Because of the URL-patterns you specified in your RewriteRule, I assume that you have placed this code, outside of any <Directory> containers, into a <VirtualHost> container in a server configuration file, and that your server is configured to resolve *both* http and https requests to this <VirtualHost> container. If any of these assumptions are incorrect, then the code will need to be modified and/or moved.

    Start-anchored patterns for the RewriteRules *must* begin with "^/" if the code is located in a server confir file outside of any <Directory> container. If located within a <Directory> container, then the value specified in that <Directory> container's path must be removed from the RewriteRule's URL-pattern. For example, if the directive is <Directory />, then remove that slash from your RewriteRule patterns.

    Similarly, in a .htaccess context, the path to "this" .htaccess file's location must be removed from the RewriteRule's URL-pattern; A RewriteRule located in "/subdir1/.htaccess" will not 'see' the "/subdir" URL_path-part, as it will have been stripped out of the variable that RewriteRule is examining. And a RewriteRule in "/.htaccess" will not 'see' that leading slash, because that is the path to its directory.

    Be aware that if you are using mod_alias or mod_proxy to "send" these requests to your back-end, then those modules may execute before mod_rewrite, and if they do, your rules will be ignored. The solution is to 'migrate' any Alias or ProxyPassReverse directive functions into the mod_rewrite code itself, so that execution order can be controlled by the sequence of directives all belonging to a single module.

    The only other tip I can think of right now is that you must delete your browser cache before testing any changes to code on your server. Otherwise, your browser will show you previously-cached pages and server response codes.

    Jim

    perotto

    4:58 pm on Mar 21, 2010 (gmt 0)

    10+ Year Member



    Thanks for all your help!

    I see this as solved for now.