Anyway to block visits to specific URLs, for eg vi

2019-08-19 20:19发布

问题:

I need to block visits to specific URLs within my site. But I am unsure how to, and is terrible at understanding the intricacies of htaccess. ^^

I need to block visits that are accessing these type of URLs, for eg.

  • http://mydomain.com/katherine-blouse-on-fire
  • http://mydomain.com/antique-for-preservation
  • http://mydomain.com/?type=extended&name=stackoverflow

My current htaccess code

DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
</IfModule>

<Files 403.shtml>
order allow,deny
allow from all
</Files>

deny from xx.xx.xx.xx 
deny from xx.xx.xx.xx 

What should I enter into htaccess to block these visits? And in which order? Before my current code or after?

回答1:

Add the below after your RewriteBase / before RewriteCond %{REQUEST_FILENAME} !\.(png|.... It will return a 403 Forbidden status code to the client.

RewritCond %{REQUEST_URI} (?:(?:katherine-blouse-on-fire)|(?:antique-for-preservation)) [NC,OR]
RewritCond %{QUERY_STRING} type\=extended&name\=stackoverflow [NC]
RewriteRule ^ - [F]

Definition:

  • Flag F apache docs.


回答2:

You may or may not wish to use mod_alias if you've got it available; the syntax is slightly more readable but it will only match paths (i.e. not query-strings):

<IfModule mod_alias.c>
Redirect 403 /katherine-blouse-on-fire
Redirect 403 /antique-for-preservation
</IfModule>