mod_aliasmod_rewritemod_rewrite$ 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
httpd.conf) or virtual host configuration..htaccess file.
Source:
mod_aliasRedirect or RedirectMatch will never have Aliases applied.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_rewriteThe 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
mod_rewritemod_rewrite.htaccessmod_rewriteExample:
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
mod_rewriteCan 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
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
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
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
<head>
<meta http-equiv="refresh" content="0; url=http://example.com/">
</head>
Sources: