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

Apache Notizen

Inhaltsverzeichnis

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:

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

Redirect checker

Example:

http://fromdual.com/mysql-consulting
301 Moved Permanently
https://fromdual.com/mysql-consulting
301 Moved Permanently
https://www.fromdual.com/mysql-consulting
301 Moved Permanently
https://www.fromdual.com/mysql-consulting/
200 OK
https://www.fromdual.com/mariadb-galera-and-mysql-consulting/
Meta Refresh

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

Apache redirect

301 - Moved Permanently

  • Simple redirects: mod_alias. It allows us to easily redirect requests from one URL to another using directives like Redirect and RedirectMatch. See Apache Module mod_alias. ~ Configuration can be done in:

  • Apache configuration: /etc/apache2/apache2.conf. NOT recommended!

  • Better in virtual host configuration: /etc/apache2/sites-available/000-default.conf

# /etc/apache2/sites-available/000-default.conf
<VirtualHost *:80>
  ServerName example.com
  # Redirect all traffic to another domain:
  Redirect 301 / https://www.anotherdomain.com/
  # Redirect individual URLs by specifying the path:
  Redirect 301 /oldlocation http://www.example.com/newlocation
</VirtualHost>

$ apachectl configtest
$ systemctl reload apache2
  • Advanced redirects: mod_rewrite. Modify request URLs dynamically, enabling advanced routing and URL restructuring. See Apache Module mod_rewrite.
$ a2enmod rewrite
$ systemctl reload apache2

<VirtualHost *:80>
  ServerName example.com
  ServerAlias www.example.com
  RewriteEngine On
  # Temporary redirects all requests from an old directory to a new one:
  RewriteRule ^old-directory/(.*)$ /new-directory/$1 [R=302,L]
  # Redirect all HTTP requests to use HTTPS:
  RewriteCond %{HTTPS} off
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
  # Redirecting non-www URLs to their www equivalent
  RewriteCond %{HTTP_HOST} !^www\. [NC]
  RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
</VirtualHost>

$ systemctl restart apache2
  • .htaccess-Datei für Apache-Weiterleitung

The .htaccess file is commonly used when you do not have access to the main server configuration file httpd.conf or virtual host configuration, which only happens if you have purchased shared hosting. You can achieve all of the above-mentioned use cases by editing the main server configuration file(s), so you should not use .htaccess when you have access to those files.

# ~.htaccess
RedirectMatch 301 "/404.shtml" "/404/index.html"
RewriteEngine on
RewriteRule ^hello.html$ goodbye.html
  • HTML page redirect
<head>
<meta http-equiv="refresh" content="0; url=http://example.com/">
</head>

Sources: