IP-based redirection using mod_rewrite

IP-based redirection using mod_rewrite - If a page has internal and external outgoing links to redirecting URLs, it’s returning 3xx (301, 302, etc.) HTTP status codes standing for redirection. This issue means that the page does not exist on a permanent or temporary basis. It appears on most of the popular web browsers, usually caused by a misconfigured website. However, there are some steps you can take to ensure the issue isn’t on your side. You can find more details about redirecting URLs by reading the Google Search Central overview. In this article, we’ll go over how you can fix the IP-based redirection using mod_rewrite error on your web browser. Problem :


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 10.8.0.11, tries to reach http://10.8.0.1/, he's properly redirected to the appropriate subfolder http://10.8.0.1/user1/.

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...).


Solution :

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 10.8.0.11 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 10.8.0.12 to user2 sub-folder
RewriteCond %REMOTE_ADDR 10.8.0.12
RewriteCond %REQUEST_URI !(user2)
RewriteRule ^(.*)$ /user2/$1 [L,R=301]

We hope that this article has helped you resolve the htaccess, redirects, mod-rewrite error in your web browsers. Enjoy browsing the internet uninterrupted!

Comments

Popular posts from this blog

How to redirect to any domain [duplicate]

"302 found" for index page bad for SEO?

Create redirect from url like www.example.us/?p=100&option=