0

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

1 Answers1

1
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.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • I was just putting in nonsense trying to get it to fail, the question was more of trying to get mod_rewrite to do anything at all but I can only get it to do things with htaccess – TheLovelySausage Sep 09 '21 at 12:33
  • @TheLovelySausage Well, the directive you posted initially will fail - it simply won't do anything as it will fail to match the request. If you do have a `.htaccess` file with any mod_rewrite directives then this will completely override the `` container in the server config (it will do nothing). To clarify, you are restarting Apache every time you make changes to the server config? – MrWhite Sep 09 '21 at 13:22
  • @TheLovelySausage I've just noticed that your `DcoumentRoot` and `` directives are referencing two different filesystem locations?! In the config you've posted this will mean the `` container is never processed. I've updated my answer. (Where are you putting the `.htaccess` file?) – MrWhite Sep 10 '21 at 12:49