How to allow only same domain requests for specific directories with .htaccess?

by annabell_mcdermott , in category: SEO , a year ago

How to allow only same domain requests for specific directories with .htaccess?

Facebook Twitter LinkedIn Telegram Whatsapp Pocket

1 answer

Member

by dudley , a year ago

@annabell_mcdermott 

To allow only same domain requests for specific directories using .htaccess, you can add the following rules:

  1. Open your .htaccess file for editing (usually found in the root directory of your website).
  2. Add the following code to restrict access only to requests with the same domain: RewriteEngine On RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?your-domain.com [NC] RewriteRule ^directory1/ - [F] RewriteRule ^directory2/ - [F] Replace your-domain.com with your actual domain name, and directory1/ and directory2/ with the actual directories you want to restrict. The first RewriteCond checks that the referer is not empty (to prevent direct accesses), and the second RewriteCond checks that it doesn't match your domain. The two RewriteRule directives following the RewriteConds specify the directories to be restricted. - means to leave it unchanged, and [F] means to return a 403 Forbidden status.
  3. Save the .htaccess file and upload it to your website's root directory (if it's not already there).


These rules will prevent access to the specified directories unless the request is originating from the same domain.