简体   繁体   中英

Silex htaccess regex block all pages except one

What i'm trying to achieve:

example.com/payment - can be accessed from anywhere. While everything else can only be accessed from certain ip.

I'm using silex 2 and apache 2.4. In case you don't know, silex uses controllers and routing, so there are no direct .php files, in this case (payment.php)

here's my .htaccess file:

<IfModule mod_rewrite.c>
##Allow profiler to be accessed from office only
RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111
RewriteRule ^(.*)_profiler(.*)$ https://www.example.com [R=301,L]

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

RewriteRule .* - [E=ENVIRONMENT:prod]

RewriteCond %{SERVER_NAME} ^stage.
RewriteRule .* - [E=ENVIRONMENT:stage]

RewriteCond %{SERVER_NAME} .dev$ [OR]
RewriteRule .* - [E=ENVIRONMENT:dev]

### PROD ###
##force www and https
RewriteCond %{ENV:ENVIRONMENT} prod
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

### STAGE ###
##force https
RewriteCond %{ENV:ENVIRONMENT} stage
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^stage\.example\.com$ [NC]
RewriteRule ^(.*)$ https://stage.example.com/$1 [L,R=301]
##deny access
RewriteCond %{ENV:ENVIRONMENT} stage
RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111
RewriteRule ^((?!payment).)*$ https://stage.example.com [R=403,L]

### DEV ###
</IfModule>

The important part is the last few lines with ##deny access for stage.

I've been pulling my hair out for the past few hours and i can't seem to get this to work. I would really appreciate if someone could help me out here.

Have a nice day ! :)

Am I right that you only want your last part to be fixed? The ##deny access lines? If not, I will expand my answer to what is needed ;)

##deny access for everything but /payment from IP != 11.111.111.111
RewriteCond %{ENV:ENVIRONMENT} stage
RewriteCond %{REMOTE_ADDR} !^11\.111\.111\.111
RewriteRule !^/payment$ - [F]

If you need /payment to be rewritten to stage.example.com add this

##allow access to /payment
RewriteCond %{ENV:ENVIRONMENT} stage
RewriteRule ^/payment$ https://stage.example.com [R=301,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM