Simplifying Magento Admin URL Rewrite
If you’re working with Magento 1.7.0.2, you might find yourself struggling to rewrite the admin URL from the default index.php/admin format to a cleaner /admin structure. In this post, we’ll guide you through the process of achieving this rewrite without any extensions or direct live site modifications.
Understanding the Current Setup
By default, Magento routes all admin requests via the index.php entry point and appends the admin path to it. This is a security measure that helps prevent direct access to the admin panel. However, for SEO purposes and aesthetic reasons, many store owners prefer a more streamlined URL.
Why You Can’t Use “Admin Rewrites” Extension
Extensions like “Admin Rewrites” typically modify the .htaccess file and alter some core Magento files. While these methods might work initially, they can cause compatibility issues with future updates or lead to unexpected behavior in the long run. We recommend avoiding such extensions for a more stable solution.
The Localhost Approach
Since you’re working on a localhost setup currently, this provides an ideal environment to test and implement changes before deploying them live. Let’s dive into the steps required to achieve the admin URL rewrite.
Step 1: Backup Your Files
Before making any modifications, always ensure you have a complete backup of your Magento installation. This includes both the database and file system backups. In case something goes wrong during the process, having a backup ensures you can quickly revert the changes.
Step 2: Modify the .htaccess File
Open your /.htaccess file located in the root directory of your Magento installation. Look for the following lines:
RewriteEngine on
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Here, we need to add a new rule for the admin requests. Add the following lines just before the last RewriteRule line:
RewriteCond %{REQUEST_URI} ^/admin$
RewriteRule ^admin$ index.php/admin/ [L,R=301]
This rule checks if the requested URI is /admin and rewrites it to index.php/admin/ with a 301 Moved Permanently status.
Step 3: Update index.php
Open your index.php file located in the root directory of your Magento installation. Look for this line:
$request = new Zend_Controller_Front();
Just before it, add the following lines to enable the admin route:
if (strpos($_SERVER['REQUEST_URI'], 'admin') !== false) {
$_SERVER['SCRIPT_NAME'] = '/index.php';
}
This code snippet checks if the requested URI contains the term admin. If it does, it sets the script name back to /index.php, allowing Magento to handle the request correctly.
Step 4: Clear Cache and Reindex
After making these modifications, clear your Magento cache and reindex all data. This ensures that any changes are properly reflected across your system.
Step 5: Test Locally
Visit http://localhost/admin in your browser. You should now be automatically redirected to the admin login page without the index.php portion in the URL.
Conclusion
Following these steps allows you to achieve a cleaner admin URL structure while maintaining the security of your Magento installation. By modifying the .htaccess file and index.php, we effectively rewrote the admin request routing without relying on any external extensions or direct live site modifications.
Remember that this solution is designed for development environments like localhost and may not be suitable for all production setups, as it alters core files. Always test changes thoroughly before deploying them to a live environment.
For more advanced customizations and security best practices in Magento, consider exploring our comprehensive guides at Laravel Company. We cover everything from theme development to performance optimization for the popular e-commerce platform.
Happy coding!