-1

I want to use .htaccess to rewrite to a folder based on the visitors IP-address. I have a list of IP-addresses with associated usernames (in a text file). The usernames are also the folder names.

  • 192.168.0.2 - John
  • 192.168.0.3 - Ben

Example: when John (IP ..0.2) goes to www.domain.com, he must see the the content of folder /John

Because the username-IP binding will be made in PHP, I prefer to use RewriteMap so I can generate a .txt file.

Unfortunately, I have to idea where to start. Rewriting based on the users IP is not the problem, the problem is finding the linked username.

Any help appreciated!

Maurice
  • 278
  • 3
  • 10

1 Answers1

1

There are several ways of doing this, but I wonder why as there are a number of existing modules that provide user directory and authentication already, anyway you could:

# Call an external program each time
RewriteMap userDir "prg:/srv/www/cgi-bin/ipToUserDirectoryMapper.php"

RewriteCond %{REQUEST_URI}  !somePatternCommonToAllAlreadyRewrittenRequests
RewriteRule .*  /${userDir:%{REMOTE_ADDR}}%{REQUEST_URI} [L,R]

where: ipToUserDirectoryMapper.php takes an IP address as an argument and returns a directory, with a default for unknown IP's.

alternitively, is the mapping is in a txt file:

# Use a static remap file
RewriteMap  userDirMap "txt:/srv/www/ipToDirectoryMap.txt"

RewriteCond %{REQUEST_URI}  !somePatternCommonToAllAlreadyRewrittenRequests
RewriteRule ^(.*)  /${userDirMap:%{REMOTE_ADDR}|someDefaultUser}%{REQUEST_URI}  [L,R]

Note: your directory names will need a common element, or you'll need to set a CGI parameter or environment variable for the user directories ( SetEnvIf Request_URI "^/John" alreadyRewritten), to prevent a redirection loop eg. call your directories UserJohn, UserBen and instead of somePatternCommonToAllAlreadyRewrittenRequests have:

 **!^/User[a-zA-Z0-9-_]+**
arober11
  • 1,969
  • 18
  • 31