SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    .htaccess redirect all urls except some and exclude my own IP

    Hi,

    I'm doing some redesign and maintenance of my site and I wanna put it in "maintenance mode" using .htaccess

    But the actual "maintenance version" of the site got 3 pages...

    So, I wanna redirect everything except those pages to the root of the domain and exclude my own IP from it...

    ...would something like this do? Or am I way off here?

    Code:
    Options +FollowSymlinks
    RewriteEngine on
    
    #urls to exclude
    RewriteCond %{REQUEST_URI} !/url-to-exclude-1$
    RewriteCond %{REQUEST_URI} !/url-to-exclude-2$
    
    #ip to allow access
    RewriteCond %{REMOTE_HOST} !^11\.11\.11\.11
    
    #send to root
    RewriteRule $ /http://www.mydomain.com [R=302,L]
    Thanks for any pointers!

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Yes that looks pretty okay to me.

    Two minor suggestions:

    1) Add a $ after the IP RewriteCond. For this example 11.11.11.11, 11.11.11.110, 11.11.11.111, 11.11.11.112, etc would all be valid, but I'm guessing you were going for just allowing 11.11.11.11

    2) Change the $ in the RewriteRule to .? which is slightly faster.

    Otherwise the code looks good. And good call with R=302, too
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    First of all, thanks for your reply! And yeah, only one IP...or several but then I just add another line as for the urls right?

    So, this would do it?

    Code:
    Options +FollowSymlinks
    RewriteEngine on
    
    #urls to exclude
    RewriteCond %{REQUEST_URI} !/url-to-exclude-1$
    RewriteCond %{REQUEST_URI} !/url-to-exclude-2$
    
    #ip to allow access
    RewriteCond %{REMOTE_HOST} !^11\.11\.11\.11$
    
    #send to root
    RewriteRule .? /http://www.mydomain.com [R=302,L]
    And I've seen when people are using an actual maintenance page they redirect to that they also exclude that one, to prevent an "infinite loop" someone wrote...is that necessary when doing like this....or will this also redirect www.mydomain.com to www.mydomain.com and so on and so on? ...or is "apache smarter than that"!?

    Then I've also seen people exclude image/css files or folders containing those, is that also necessary and if so why?

    And thanks again for taking the time!

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by kungknas View Post
    First of all, thanks for your reply! And yeah, only one IP...or several but then I just add another line as for the urls right?
    Yes

    Quote Originally Posted by kungknas View Post
    And I've seen when people are using an actual maintenance page they redirect to that they also exclude that one, to prevent an "infinite loop" someone wrote...is that necessary when doing like this....or will this also redirect www.mydomain.com to www.mydomain.com and so on and so on? ...or is "apache smarter than that"!?
    Good point, you should include the / URL, otherwise you will indeed get an infinite loop!

    Quote Originally Posted by kungknas View Post
    Then I've also seen people exclude image/css files or folders containing those, is that also necessary and if so why?
    Because otherwise the images and css files etc are also redirected to / since Apache doesn't conscern between content type, it will just rewrite any URL you throw at it.

    So you'd end up with something like

    Code:
    Options +FollowSymlinks
    RewriteEngine on
    
    #urls to exclude
    RewriteCond %{REQUEST_URI} !/url-to-exclude-1$
    RewriteCond %{REQUEST_URI} !/url-to-exclude-2$
    RewriteCond %{REQUEST_URI} !^/images$
    RewriteCond %{REQUEST_URI} !^/css$
    RewriteCond %{REQUEST_URI} !^/$
    
    #ip to allow access
    RewriteCond %{REMOTE_HOST} !^11\.11\.11\.11$
    
    #send to root
    RewriteRule .? /http://www.mydomain.com [R=302,L]
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Works like a charm!

    Thanks a lot!

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Rémon,

    Why did you allow the / before http: in the RewriteRule's redirection? Apparently it's being ignored but I expected it to cause a problem (it should).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,083
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I'm pretty sure that's a slip of the fingers
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •