Pingu
Computer MySQL PostgreSQL Books Publications
Spielereien Kanu Business TopoDB POI Klettersteigen History TransPool Thermal Baden Brokenstuben Goldwaschen
Blog Contact
Shinguz
Google
/ch/open

Apache Notizen

Apache Module abfragen

$ a2query -m rewrite
rewrite (enabled by site administrator)

$  a2query -m rewrite
No module matches rewrite

$ a2enmod rewrite
Enabling module rewrite.
To activate the new configuration, you need to run:
  systemctl restart apache2

Redirect / Rewrite

  • Prio 1: Do in server configuration (httpd.conf) or virtual host configuration.
  • Prio 2: Do in .htaccess file.
    1. Worse performance
    2. Bad for security

Source: A beginner’s guide to creating redirects in an .htaccess file

mod_alias

  1. Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied.
  2. The Aliases and Redirects are processed in the order they appear in the configuration files, with the first match taking precedence.

Example:

Redirect permanent "/one" "http://example.com/two"
Redirect 301 "/three" "http://example.com/other"
RedirectMatch permanent "(.*)\.gif$" "http://other.example.com$1.jpg"

Source: Apache Module mod_alias

mod_rewrite

The main difference is that, with mod_alias, the server is responding to the client request with a redirect, so the client immediately is sent to the new location. Conversely, with mod_rewrite, the server simply returns the new content, so the client is not actually redirected anywhere. This makes mod_rewrite more advantageous because it happens transparently, requiring less work from the client (user).

Source: Difference between mod_alias and mod_rewrite

RewriteEngine  on
RewriteRule    "^/foo\.html$"  "/bar.html" [PT]

Execution order:

do
  execute server and vhost rewrites (in the Apache Virtual Host Config)
  find the lowest "Per Dir" .htaccess file on the file path with rewrites enabled
  if found(.htaccess)
     execute .htaccess rewrites (in the user's directory)
while rewrite occurred

Debugging mod_rewrite

Can be set in server configuration (httpd.conf), virtual host configuration or directory configuration but not in .htaccess:

LogLevel alert rewrite:trace3

Then restart Apache:

$ systemctl restart apache2
$ tail -f error_log|fgrep '[rewrite:'

Source: Tips for debugging .htaccess rewrite rules