2

I want the content of a directory "dir" protected, but if the user accesses the directory itself, I want a PHP file from the parent directory served instead. This works with the following configuration:

<VirtualHost *:80> LogLevel debug DocumentRoot "/var/www/html" ServerName example.com <Directory /var/www/html> SetEnvIf Request_URI "dir/$" allow Order allow,deny Allow from env=allow Satisfy any RewriteEngine on RewriteRule dir index.php?id=dir [QSA,L] </Directory> <Directory /var/www/html/dir> Require user valid-user AuthName "Restricted" AuthType Basic AuthUserFile /etc/apache2/.htpasswd #RewriteEngine on </Directory> </VirtualHost> 

When I access "example.com/dir" the content of index.php is displayed, when I access "example.com/dir/file.html" I have to authenticate first. Everything is as wanted.

But when I now try to create RewriteRules for other files, I quits working. When I just uncomment the line "#RewriteEngine on" at the end, I get a 404 not found when I try to access "example.com/dir".

Why does this happen? I don't need other rewrite rules for "dir" itself but for some files within the directory.

1 Answer 1

0

But when I now try to create RewriteRules for other files, I quits working. When I just uncomment the line "#RewriteEngine on" at the end, I get a 404 not found when I try to access "example.com/dir".

Because mod_rewrite directives are not inherited by default in a directory context. When you set RewriteEngine On in the <Directory /var/www/html/dir> container it completely overrides the mod_rewrite directives in the parent (they do not even run).

To resolve this as you have written it then you would need to enable mod_rewrite inheritance. However, mod_rewrite directives are essentially copied in-place when they are "inherited", and the directive you posted would not work if it was included in the <Directory /var/www/html/dir> container, since the regex dir would not match.

RewriteRule dir index.php?id=dir [QSA,L] 

Note that this doesn't just rewrite requests to the directory itself, it rewrites any request that simply contains dir.

To make this "inheritable" (ie. not dependent on the current directory), change it to read:

RewriteCond %{REQUEST_URI} ^/(dir)/?$ RewriteRule ^ /index.php?id=%1 [QSA,L] 

And in the <Directory /var/www/html/dir> container, enable mod_rewrite inheritance. Either:

RewriteEngine On # Parent directives are copied AFTER directives in the current scope RewriteOptions Inherit 

Or

# Parent directives are copied BEFORE directives in the current scope RewriteOptions InheritBefore 

(Apache 2.2 only have the inherit option.)

2
  • Thank you for trying to help. Unfortunately adding RewriteOptions does not change anything. Just uncommenting "RewriteEngine on" or adding any of the two "RewriteOptions" results in a 404 message for "example.com/dir". My .config file above should be a complete example to reproduce (Apache 2.4.41 Ubuntu 20.04) Commented Jan 28, 2021 at 9:31
  • I forgot the log message: It is "AH00129: Attempt to serve directory: /var/www/html/dir/" Commented Jan 28, 2021 at 9:45

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.