0

Assume the following folder structure on the webserver (Apache): https://somedomain.com/parentfolder/childfolder.

Using .htaccess, I'd like to display the childfolder and any of it's children (files as well as folders) only the URL contains a certain parameter. For example, the page should be shown if it's requested by:

https://somedomain.com/parentfolder/childfolder?secretparameter=secretvalue123 

However if

  • the value of parameter secretparameter is not exactly secretvalue123 or
  • the parameter secretparameter is missing,

then a 404 error should be returned.

Do you have any ideas how the .htaccess rules would look like to accomplish this? Your help is greatly appreciated, thank you.

1 Answer 1

1

So, if the query string is not secretparameter=secretvalue123 when requesting a resource within the childfolder then issue a 404 not found. You can do something like the following at the top of the .htaccess file in the document root using mod_rewrite:

RewriteEngine On RewriteCond %{QUERY_STRING} !=secretparameter=secretvalue123 RewriteRule ^parentfolder/childfolder - [R=404] 

This blocks the request if the query string is not exactly equal to secretparameter=secretvalue123. (You don't need the L flag here when specifying a non-3xx code as it is implied.)

You could simplify the RewriteRule slightly if instead, you placed these directives in the subdirectory you want to protect. ie. at /parentfolder/childfolder/.htaccess. For example:

RewriteRule ^ - [R=404] 
2
  • Thank you for the explanation! I don't know why, but it's just not working: _https://somedomain.com/parentfolder/childfolder can still be accessed through the browser, regardless if secretparameter=secretvalue123 is part of the URL or not. Looks like something's wrong here... Well, there's an index.php file at that location. Could this be the cause of the failing redirect? Commented Sep 27, 2018 at 23:28
  • Try (temporarily) removing the RewriteCond directive (comment out with a #) - is it blocked now? An index.php file itself is not the problem, however, do you have any other .htaccess files that also contain mod_rewrite directives? Commented Sep 28, 2018 at 8:24

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.