When trying to implement simple redirects using mod_alias
directives in .htaccess
, you might find they don't work as expected. Before troubleshooting the rules themselves, it's crucial to verify whether the module is actually loaded in your Apache configuration.
Method 1: Using httpd -M
httpd -M | grep alias
# Expected output if enabled:
# alias_module (shared)
Method 2: Checking loaded_modules in PHP
<?php
print_r(apache_get_modules());
?>
# Look for 'mod_alias' in the output array
For shared hosting where command line access isn't available:
# Create a test.php file with:
<?php
phpinfo();
?>
Then search for "Loaded Modules" in the output and look for mod_alias
.
If you've confirmed mod_alias is enabled but still having issues, try these basic tests:
# Simple redirect test
Redirect 301 /oldpage.html http://example.com/newpage.html
# Alias test
Alias /images /path/to/your/images
- Check Apache error logs for module-related errors
- Ensure AllowOverride includes FileInfo for .htaccess
- Verify the module isn't being overridden by mod_rewrite rules
mod_alias | mod_rewrite |
---|---|
Simple redirects | Complex URL manipulation |
No regex required | Regex pattern matching |
Lower overhead | More flexible but heavier |
mod_alias is an Apache module that provides simple URL manipulation through directives like Redirect, RedirectMatch, Alias, and ScriptAlias. Unlike mod_rewrite which offers more complex pattern matching, mod_alias is perfect for straightforward redirect scenarios.
# Example basic mod_alias usage
Redirect 301 /old-page.html https://example.com/new-page.html
RedirectMatch 301 ^/old-dir/(.*)$ https://example.com/new-dir/$1
There are several ways to check if mod_alias is enabled on your Apache server:
Method 1: Using apachectl/httpd Command
# For most Linux systems
apachectl -M | grep alias
# or
httpd -M | grep alias
# Expected output if enabled:
alias_module (shared)
Method 2: Checking Configuration Files
Inspect your Apache configuration files (typically httpd.conf or apache2.conf) for the LoadModule directive:
grep -r "LoadModule alias_module" /etc/apache2/
Method 3: PHP Info (For Shared Hosting)
If you don't have shell access, create a PHP file with:
<?php phpinfo(); ?>
Then search for "Loaded Modules" in the output.
If you discover mod_alias isn't enabled, here's how to activate it:
# On Debian/Ubuntu
sudo a2enmod alias
sudo systemctl restart apache2
# On CentOS/RHEL
# Edit /etc/httpd/conf/httpd.conf and uncomment:
LoadModule alias_module modules/mod_alias.so
# Then restart Apache
systemctl restart httpd
Even when mod_alias is enabled, directives might not work due to:
- Incorrect file permissions on .htaccess
- AllowOverride not set to FileInfo or All
- Conflicts with mod_rewrite rules
- Syntax errors in the directives
# Redirect single page
Redirect 301 /about.html /about-us.html
# Redirect entire directory
RedirectMatch 301 ^/products/(.*)$ /catalog/$1
# Alias for static files
Alias /downloads "/var/www/special-downloads"
<Directory "/var/www/special-downloads">
Require all granted
</Directory>
Remember that mod_alias directives execute before mod_rewrite rules, which can affect your expected behavior if both modules are used together.