I'm running a small VPN server where each user has his own static IP.
I set up an Apache server in order to host files for each of them and instead of using a new registration system, I wanted to take advantage of those static IPs.

So I thought I could use mod_rewrite to achieve that and wrote this snippet:

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} 10\.8\.0\.11
RewriteRule ^(.*)$ user1/ [L]

If user1, who has the IP, tries to reach, he's properly redirected to the appropriate subfolder
If user1 tries to reach any other subfolder, he's also properly redirected to his own subfolder.

Though there are some issues with this setup:

  • if there's an index file on that folder, I end up with an internal error
  • if there's any other file, I cannot download/run it

Is there any way I can overcome this?
I mean, locking a user on a specific subfolder based on his IP and allowing him to browse that folder (viewing a website, retrieving files...).

1 Answer 1


I'm sure there must be a more elegant/efficient way of writing this, but this should work to do what you require:

RewriteEngine On
RewriteBase / 

# Restrict users from IP address to user1 sub-folder
RewriteCond %{REMOTE_ADDR} 10\.8\.0\.11 
RewriteCond %{REQUEST_URI} !(user1)
RewriteRule ^(.*)$ /user1/$1 [L,R=301]

# Restrict users from IP address to user2 sub-folder
RewriteCond %{REMOTE_ADDR} 10\.8\.0\.12 
RewriteCond %{REQUEST_URI} !(user2)
RewriteRule ^(.*)$ /user2/$1 [L,R=301]
  • Thanks! Though these rules trigger an "Internal Server Error". The server doesn't like the added $1 on the second one nor running without a trailing slash on the first one.
    – shroom
    May 21, 2015 at 13:44
  • 1
    From testing I found the code I'd added initially was causing an infinite redirect loop, which after the maximum redirects count would generate the HTTP 500 Internal Server Error message. I've now troubleshooted and tested and can confirm the amended solution above should now work for you. May 28, 2015 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.