Apache2 mod_rewrite seemingly not doing anything
I'm having difficulty getting mod_rewrite to work in Apache2 on Debian 10. I enabled the extension with
a2enmod rewrite
systemctl restart apache2
And had no errors and can see the module in
apachectl -M
...
rewrite_module (shared)
...
Although when I add it to my vhost in the sites-available
<VirtualHost *:80>
ServerName default.nothing
ServerAlias www.default.nothing
DocumentRoot /var/www/html/public_html/00-default
<Directory "/volume/dev/html/public_html/00-default">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine on
RewriteRule ^192.168.20.87$ nothing
</Directory>
<IfModule mpm_user_module>
ServerEnvironment apache apache
</IfModule>
</VirtualHost>
Hoping that it would rewrite the url http://192.168.20.87/page.php as http://nothing/page.php in the browser tab. No matter what I put into RewriteRule nothing seems to happen. I'm sure I'm doing something
RewriteEngine on RewriteRule ^192.168.20.87$ nothing
For some reason if I do it with .htaccess it works
Although this won't match the URL http://192.168.20.87/page.php
even if you "do it with .htaccess
", so you must be doing something else?
The above rule (when used in a <Directory>
container) matches a URL of the form http://192.168.20.87/192.168.20.87
(or http://default.nothing/192.168.20.87
- if your hostnames resolve).
The RewriteRule
pattern matches the URL-path only, not the hostname (ie. 192.168.20.87
). So, this matches against /page.php
, (or page.php
- relative path/no slash prefix - when used in a directory context like <Directory>
or .htaccess
.)
So this would need to be like the following instead:
RewriteRule ^page\.php$ nothing
(Although it's unclear what you are trying to do here, what is "nothing"? If you are trying to trigger a 404 then this is not really the way to do it.)
As @Gerrit loosely mentioned in comments, when the RewriteRule
directive is used in a server or virtualhost context (ie. not in <Directory>
or .htaccess
containers - a directory context) then the URL-path matched by the RewriteRule
directive is root-relative, starting with a slash, because the directive is processed much earlier, before it has been mapped to the file system. eg. ^/page\.php$
.
UPDATE:
DocumentRoot /var/www/html/public_html/00-default <Directory "/volume/dev/html/public_html/00-default">
I've just noticed that your DocumentRoot
and <Directory>
directives are referencing two different filesystem locations?! With the limited information in the question, this <Directory>
container is never going to be processed.
But unless you have additional directives elsewhere (which I assume you must have) then any .htaccess
file placed in the document root (which is where I assume you are putting it) is never going to be processed either.