Skip to main content

Working with Aliases and Redirects

1. Creating Path Aliases

Directive Description Example
Alias Maps a URL path to a filesystem path <Alias /images/ "/var/www/images/">
AliasMatch Maps a URL path using a regular expression <AliasMatch ^/images/(.*)$ "/var/www/images/$1">
Redirect Performs a redirect from one URL to another <Redirect /old-page.html /new-page.html>

Example: Alias Directive Usage

<VirtualHost *:80>
    ServerName example.com
    Alias /images/ /var/www/images/
    <Directory "/var/www/images/">
        Options Indexes FollowSymLinks
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>

Example: AliasMatch with Regex

<AliasMatch ^/static/(.*)$ "/var/www/static/$1">

2. Using Script Aliases

Script aliases in Apache HTTP Server allow you to map a URL path to a directory containing executable scripts. These aliases are particularly useful for running CGI scripts or other server-side executables.

Directive Description Example
ScriptAlias Maps a URL prefix to a directory of scripts ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
ScriptAliasMatch Maps a URL prefix to a directory using a regular expression ScriptAliasMatch ^/cgi-bin/(.*)$ /usr/lib/cgi-bin/$1

Example: Setting up a Script Alias

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI
    Require all granted
</Directory>
Note: The Options +ExecCGI directive is required to allow CGI scripts to be executed from the specified directory.
Warning: Ensure that the directory containing scripts has appropriate permissions to prevent unauthorized access.

3. Configuring Simple Redirects

Directive Values Description
Redirect 301 /old-path /new-path Permanently redirects from old path to new path
Redirect 302 /old-path /new-path Temporarily redirects from old path to new path
Redirect permanent /old-path /new-path Alias for 301 redirect
Redirect temp /old-path /new-path Alias for 302 redirect

Example: Basic Redirect

<VirtualHost *:80>
    Redirect 301 /old-page.html /new-page.html
</VirtualHost>

Example: Redirect with Wildcard

<VirtualHost *:80>
    Redirect 301 /blog/* /articles/*
</VirtualHost>

4. Using Permanent Redirects

Permanent redirects are used to inform browsers and search engines that a resource has permanently moved to a new location. These redirects return an HTTP 301 Moved Permanently status code.

Directive Description Example
Redirect Permanently redirects a URL to another URL. Redirect permanent /old-page.html /new-page.html
RedirectMatch Redirects based on a regular expression pattern. RedirectMatch permanent ^/old/(.*)$ /new/$1

Example: Redirecting a Single Page

Redirect permanent /about.html /about-us.html

Example: Redirecting with Regex

RedirectMatch permanent ^/blog/(.*)$ /articles/$1
Note: The Redirect directive is processed before other directives like Alias or Directory. Ensure that redirect rules are placed appropriately to avoid conflicts.

5. Using Temporary Redirects

Temporary redirects are used to redirect clients to a different URL temporarily, typically for short-term changes or maintenance.

Directive Description Example
Redirect Performs a redirect to a new URL. The status code is 302 (temporary) by default. Redirect /old-page.html /new-page.html
RedirectMatch Redirects based on a regular expression pattern. RedirectMatch 302 ^/old/(.*)$ /new/$1
RedirectPermanent Performs a permanent redirect (301 status code). Not used for temporary redirects. RedirectPermanent /old-page.html /new-page.html

Example: Redirecting a specific path to another URL

Redirect /old-page.html /new-page.html

Example: Redirecting with a regex pattern

RedirectMatch 302 ^/old/(.*)$ /new/$1
Note: The Redirect directive is processed before other directives like Alias or Directory. It is best used for simple path-to-path redirections.
Warning: Using Redirect without a leading slash (e.g., Redirect old-page.html /new-page.html) can lead to unexpected behavior or incorrect redirects.

6. Using Pattern-Based Redirects

Pattern-based redirects in Apache use the RewriteRule directive with pattern matching to redirect requests based on URL components. These are useful for handling complex URL structures or migrating content.

Directive Parameters Description
RewriteRule pattern replacement [flags] Defines a rewrite rule with pattern matching and replacement logic

Example: Redirecting URLs with Pattern Matching

RewriteRule ^old-page/(.*)$ /new-page/$1 [R=301,L]

Example: Redirecting with Query String Handling

RewriteRule ^products/([^/]+)/?$ /items/$1 [R=301,L]
Note: The [R=301] flag indicates a permanent redirect, while [R=302] is for temporary redirects.

Key Flags for Pattern-Based Redirects

  • R=301 - Permanent redirect
  • R=302 - Temporary redirect
  • L - Last rule (stops processing further rules)
  • QSA - Query String Append (preserves query parameters)

7. Returning Gone Status

Directive Description Usage
Redirect Permanently redirects a URL to another location with a 301 status code Redirect permanent /old-path /new-path
RedirectMatch Redirects based on a regular expression pattern RedirectMatch permanent ^/old/(.*)$ /new/$1
RedirectTemp Temporarily redirects a URL with a 302 status code Redirect temp /temp-path /new-temp-path

Example: Redirecting a Gone Resource

Redirect permanent /api/v1/users /api/v2/users
Note: Use Redirect permanent to indicate that the resource is permanently moved and should not be accessed via the old URL.

8. Combining Alias with Directory Blocks

Directive Description Example
Alias Maps a URL prefix to a filesystem path Alias /images/ "/var/www/images/"
Directory Defines a configuration block for a specific directory <Directory "/var/www/images/">

Example: Combining Alias and Directory

<VirtualHost *:80>
    Alias /images/ "/var/www/images/"

    <Directory "/var/www/images/">
        Options -Indexes
        AllowOverride None
        Require all granted
    </Directory>
</VirtualHost>
Note: When combining Alias with Directory, ensure the directory path matches exactly to avoid misconfigurations.

9. Understanding Alias vs Rewrite Differences

Aspect Alias Rewrite
Purpose Maps a URL path to a filesystem path Changes the URL path internally or externally
URL Visibility URL remains unchanged URL can change (internal or external)
Processing Location Before normal file system lookup After normal file system lookup
Use Case Static content mapping (e.g., /images/ to /var/www/images/) Dynamic URL manipulation (e.g., /old-page.html to /new-page.html)
Performance Better performance due to early handling May have slight overhead due to rule processing
Syntax Example Alias /images/ /var/www/images/ RewriteRule ^old-page\.html$ /new-page.html [R=301,L]

Example: Alias Usage

<VirtualHost *:80>
    Alias /images/ /var/www/images/
    <Directory "/var/www/images/">
        Require all granted
    </Directory>
</VirtualHost>

Example: RewriteRule Usage

<VirtualHost *:80>
    RewriteEngine On
    RewriteRule ^old-page\.html$ /new-page.html [R=301,L]
</VirtualHost>

Key Takeaways

  • Alias is for mapping URLs to file system paths directly.
  • Rewrite is for URL manipulation, including redirects and rewrites.
  • Use Alias for static content and RewriteRule for dynamic URL changes.

10. Testing Redirects

Directive Description Example
Redirect Permanently redirects a URL to another location <Redirect permanent /old-page.html /new-page.html>
RedirectMatch Redirects based on a regular expression pattern <RedirectMatch permanent ^/old/(.*)$ /new/$1>
RedirectTemp Temporarily redirects a URL to another location <RedirectTemp /temp-page.html /new-temp-page.html>

Example: Testing Redirects with curl

curl -I http://example.com/old-page.html

Expected response:

HTTP/1.1 301 Moved Permanently
Location: http://example.com/new-page.html
Note: Use -I flag with curl to fetch only headers and avoid downloading the redirected page content.

Example: RedirectMatch Testing

<VirtualHost *:80>
    RedirectMatch permanent ^/old/(.*)$ /new/$1
</VirtualHost>

Testing:

curl -I http://example.com/old/page.html

Expected response:

HTTP/1.1 301 Moved Permanently
Location: http://example.com/new/page.html

11. Avoiding Redirect Loops

Note: Redirect loops occur when a redirect rule points back to itself or creates a cycle, causing the browser to endlessly redirect.
Problem Solution Example
Looping redirect Ensure redirect rules do not point back to the original URL
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/old-page$
RewriteRule ^/old-page$ /new-page [R=301,L]
</IfModule>
Infinite redirect chain Use R=301 with proper conditions to prevent cycles
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/redirect$
RewriteRule ^/redirect$ /final-page [R=301,L]
</IfModule>

Example: Preventing Redirect Loops

<IfModule mod_rewrite.c>
RewriteEngine On
# Prevent loop by ensuring the target is not the same as source
RewriteCond %{REQUEST_URI} ^/old-path$
RewriteRule ^/old-path$ /new-path [R=301,L]
</IfModule>
Warning: Using R=302 (temporary redirect) can mask loop issues in development but cause problems in production.

Best Practices to Avoid Redirect Loops

  • Always test redirects in a staging environment before deploying to production.
  • Use R=301 for permanent redirects to avoid browser caching issues.
  • Ensure that redirect targets do not match the original URL pattern.

12. Managing Multiple Redirects

Directive Parameters Description
Redirect [/path] [target] Redirects a URL path to another location
RedirectMatch [regex] [target] Redirects based on a regex pattern match
RedirectPermanent [/path] [target] Issues a 301 permanent redirect
RedirectTemp [/path] [target] Issues a 302 temporary redirect

Example: Redirecting Multiple Paths

<VirtualHost *:80>
    Redirect /old-page /new-page
    Redirect /legacy-doc /docs/latest
    RedirectMatch ^/api/v1/(.*)$ /api/v2/$1
</VirtualHost>
Note: Redirects are processed in order, so more specific redirects should come before general ones to avoid conflicts.

Was this article helpful?