I have the following .htaccess
file:
RewriteEngine On
RewriteCond ${ipmap:%{REMOTE_ADDR}} ^allow$ [NC]
RewriteRule ^ - [F,L]
I would like to know if it's possible to redirect the IP's from the list to another folder but they still see the same URL (example.com
) and not example.com/folder
.
CodePudding user response:
You could do something like this:
RewriteEngine On
# Rewrite requests to "/folder" for specific IPs
RewriteCond ${ipmap:%{REMOTE_ADDR}} ^allow$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^folder/ folder%{REQUEST_URI} [L]
You shouldn't need the NC
on the rule that matches from your rewrite map. (Assuming your map is consistently using allow
and not any other mixed case version of this?)
If everything is to be rewritten to /folder
and you aren't referencing any static resources outside of /folder
then you can perhaps remove the two conditions that perform the filesystem checks (to test whether the request does not already map to a file or directory).
As an added bonus, you can prevent direct user access to the /folder
and redirect back to the root with something like the following before the above rewrite (immediately after the RewriteEngine
directive):
# Redirect direct requests to "/folder" back to root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^folder(?:$|/(.*)) /$1 [R=301,L]
(NB: This does assume you don't have another .htaccess
file in the /folder
subdirectory that contains mod_rewrite directives. If you do then you would need to preform this redirect in that .htaccess
file instead.)