I'm trying to configure apache to redirect the requests containing only hostname (domain) to another path. I found the following directives useful, but as long as those are enclosed in VirtualHost
, put in site_available
folder ( /etc/apache2/
) and enabled as a separate site (with a2ensite
command).
<VirtualHost *:80>
ServerName myserver:80
WSGIScriptAlias / /var/path/to/myproject/myproject.wsgi
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ /path/path [R=301,L]
</VirtualHost>
Note that only Rewrite*
directives don't work. The alias to my Python project works fine.
Morover, I found out that if I disable other virtual host(s) (000-default) with a2dissite
command and put the directives mentioned above (without VirtualHost
) to httpd.conf file everything (including the Rewrite*
rules) work.
So, I am in doubt why I couldn't get this done in simple server config context, and what is the solution?
The reason why it appears that the Rewrite*
directives do not work in the server configuration and need to be declared inside the VirtualHost
is because (from the RewriteEngine
documentation):
[...] rewrite configurations are not inherited by virtual hosts. This means that you need to have a
RewriteEngine on
directive for each virtual host in which you wish to use rewrite rules.
To change this behaviour, you can set RewriteOptions
to something InheritDownBefore
or similar. You then still need to have a RewriteEngine On
on each VirtualHost
but at least you won't need to copy the rewrite rules on each one.
So, taking your code example, this should work:
RewriteEngine On
RewriteOptions InheritDownBefore
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ /path/path [R=301,L]
<VirtualHost *:80>
ServerName myserver:80
WSGIScriptAlias / /var/path/to/myproject/myproject.wsgi
RewriteEngine On
</VirtualHost>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.