Search code examples
.htaccess

.htaccess redirect all request to subfolder only if not specifed ip


I cant figure out how to redirect all requests except my ip to a subfolder in .htaccess

I came up with

RewriteEngine On

RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteRule ^$ /coming-soon/ [L]
RewriteRule (.*) http://example.com/coming-soon [R=301]

But when i write in browser for example example.com/asdasdas it gives me

coming-soon/coming-soon/coming-soon/coming-soon/coming-soon/coming-soon/coming-soon/coming-soon

What i would like to achieve is when user enters anything that it redirects to example.com/coming-soon unless it is my IP. I did research on SO, but I always get stuck.

Also there is a folder in the root /coming-soon with images and fonts for that html

Any help would be appreciated


Solution

  • You can use this :

    RewriteEngine On
    
    RewriteCond %{REMOTE_ADDR} !=123.45.67.89
    RewriteCond %{REQUEST_URI} !^/coming-soon [NC]
    RewriteRule (.*) http://example.com/coming-soon [R=301,L]
    

    Make sure to clear your browser cache or use a different browser to test this code.