Solving Magento URL Rewrite Issues on Ubuntu
The Problem: Broken Links Beyond Homepage
You’ve installed Magento on your Ubuntu server under /var/www/magento. However, clicking any link other than the homepage results in a “Not Found” page displayed by Apache. For instance:
- Navigating to
http://localhost/magentoworks fine. - Clicking on a navigation bar item like “foo” takes you to
http://localhost/magento/foo.html. - This URL displays an Apache 404 error page.
The issue lies in the rewrite rules for URLs other than the homepage. Currently, Magento is unable to process these rewritten URLs properly without including index.php.
Understanding Your Configuration
Your configuration looks good so far:
System -> Configuration -> (General ->) Web:
Use Web Server Rewrites: YES
Base URL: http://localhost/magento
You’ve enabled URL rewriting and set the base URL correctly. However, something is amiss with your mod_rewrite settings.
Troubleshooting Steps
1. Review Your .htaccess File
Verify that your .htaccess file in the Magento root directory contains:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /magento/
RewriteRule ^index.php/ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/setup.*
RewriteRule . /index.php [L]
</IfModule>
2. Check Your Apache Configuration
Ensure your Apache configuration file(s) in /etc/apache2/ have the following settings:
<Directory /var/www/magento>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
3. Configure Your Virtual Host File
Modify your main virtual host file in /etc/apache2/sites-enabled/000-default.conf or /etc/apache2/sites-available/your_virtual_host.conf:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/magento
<Directory "/var/www/magento">
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
4. Restart Apache for Changes to Take Effect
After making these changes, restart your Apache server:
sudo service apache2 restart
The Solution: Enabling AllowOverride in Your Directory Configuration
The key step is to set AllowOverride to All within the directory configuration tags for your Magento installation. This allows Magento’s .htaccess file to override the default server settings.
Open the main Apache configuration file (usually /etc/apache2/apache2.conf) and ensure the following is present:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
If this setting isn’t present, add it to your configuration file. Save the changes and restart Apache:
sudo service apache2 restart
Conclusion
Ensuring proper .htaccess files are in place, reviewing your Apache configuration for AllowOverride, and setting up your virtual host correctly should resolve your Magento URL rewrite issues.
Remember, enabling AllowOverride All can potentially slow down your server and introduce security risks. Only enable it where necessary, monitor performance, and keep your system secure.
For more information on optimizing Magento performance and security, check out our blog on best practices at Laravel Company.