Question

Restrict 301 redirects by IP

Posted December 29, 2021 140 views
Apache

It’s me again! Recently I posted in the community about some specific 301 redirects and it seems I’m again stuck on this topic.

I’m generating URLS for redirection with .htaccess and what I need to add now is to some specific redirects to be accessed only by a specific IP address.

Let’s say I want to have all redirects to a specific folder redirected/accessed by only a specific IP.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @brendRos,

I’m glad to see you using the community more and more that’s really cool!

As for your question, you can do something like the following:

RewriteEngine On

RewriteCond %{REMOTE_ADDR} !=XXX.XXX.XXX.XXX
RewriteRule ^RedirectedFolder($|/) - [F]

where XXX.XXX.XXX.XXX is your IP address. For example, if your IP address was 10.32.43.89, the block will look like this:

RewriteEngine On

RewriteCond %{REMOTE_ADDR} !=10.32.43.89
RewriteRule ^RedirectedFolder($|/) - [F]

Also RedirectedFolder is the folder you mentioned you want to be redirected at.

  • I’ve added the redirect and added the correct IP but for some reason it’s not working. Most probably I’m missing something but not sure what it is.

    For reference, I’ve added the rule at the bottom of the .htaccess file.

    • You need to add the RewriteCondition and Rule to the top of the .htaccess otherwise other conditions might trigger the RewriteCond.

      • Thank you!

        I tried to add but it’s still the same outcome, not working.

        • Hmm, are you by anychance using a CDN or something similar?

          If you are using a CDN (eg. Cloudflare), load balancer or front-end caching proxy then you may need to check another element of the request since the client is not connecting directly with your server