Score:0

Apache2 mod_rewrite seemingly not doing anything

lk flag

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

TheLovelySausage avatar
lk flag
For some reason if I do it with .htaccess it works
Gerrit avatar
cn flag
In the vhost config you have to use a `/` as the first character of the regex. Sometimes you see `/?url` regexes that are meant to work in both a vhost or a .htaccess.
kz flag
@Gerrit Not when used inside a `<Directory>` container (ie. a _directory_ context), as shown in the question.
Score:1
kz flag
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.

TheLovelySausage avatar
lk flag
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
kz flag
@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 `<Directory>` container in the server config (it will do nothing). To clarify, you are restarting Apache every time you make changes to the server config?
kz flag
@TheLovelySausage I've just noticed that your `DcoumentRoot` and `<Directory>` directives are referencing two different filesystem locations?! In the config you've posted this will mean the `<Directory>` container is never processed. I've updated my answer. (Where are you putting the `.htaccess` file?)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.