Moving Magento to a New Server: Solving Automatic Redirect Issues
Hello developers! Today I’m going to help you troubleshoot a common issue faced when moving your Magento store to a new server - the automatic redirection from the subdirectory to the main domain. Let’s dive into this problem and find a solution.
The Problem: Redirecting from Subdirectory to Main Domain
You’ve successfully moved your Magento files and database to a new server, but now you’re facing an issue where the frontend redirects automatically from newserver.com/shop to newserver.com. You’ve tried editing base URL settings in the admin panel and flushing caches, but nothing seems to work. What’s going on?
Checking Your .htaccess File
The first step is to examine your .htaccess file on the new server. Here’s a typical configuration:
DirectoryIndex index.php
<IfModule mod_php5.c>
php_flag magic_quotes_gpc off
php_flag session.auto_start off
php_flag suhosin.session.cryptua off
php_flag zend.ze1_compatibility_mode Off
</IfModule>
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
<IfModule mod_deflate.c>
</IfModule>
<IfModule mod_ssl.c>
SSLOptions StdEnvVars
</IfModule>
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
#RewriteBase /magento/ (I have tried to change it to /shop/ and uncomment and still redirect me)
#RewriteRule ^api/([a-z][0-9a-z_]+)/?$ api.php?type=$1 [QSA,L]
## rewrite API2 calls to api.php (by now it is REST only)
RewriteRule ^api/rest api.php?type=rest [QSA,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
#RewriteCond %{REQUEST_URI} !^/mobiledirectoryhere/.*$
#RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
#RewriteRule ^(.*)$ /mobiledirectoryhere/ [L,R=302]
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
The Culprit: Incorrect Base URL Settings
The issue lies in the base URL settings within your Magento configuration. When you moved your site, you likely updated the base_url and unsecure_base_url fields in the database using a tool like MySQL or phpMyAdmin.
However, it’s crucial to ensure these values are set correctly:
- For
base_url: This should match your secure URL, including the scheme (http://orhttps://). If you’re running on an insecure scheme (i.e., HTTP), Magento will redirect all requests to HTTPS. - For
unsecure_base_url: This should match your unsecured URL structure. It’s important to note that for a single domain with both secure and non-secure URLs, this field should not include thehttps://prefix.
The Solution: Update Base URL Configuration
To resolve the automatic redirection issue:
- Log in to your MySQL database using a client like phpMyAdmin or Navicat.
- Locate the
core_config_datatable. - Search for records with
path = 'web/unsecure/base_url'andpath = 'web/secure/base_url'. - Update these values to match your new server’s URL structure, ensuring they include no trailing slashes.
Here’s an example of how you might update the base URLs:
- For secure (HTTPS):
https://newserver.com/shop - For unsecured:
http://newserver.com/shop
Testing the Solution
After updating your database configuration:
- Flush Magento’s cache from the admin panel (System > Cache Management).
- Clear your browser cache.
- Visit
http://newserver.com/shopand see if you’re no longer redirected to the main domain.
If everything is set up correctly, you should now be able to access your Magento store at its intended URL without any automatic redirections occurring.
Conclusion
Moving a Magento site between servers can sometimes lead to unexpected issues like automatic redirection. In this case, ensuring your base URL configuration matches the new server’s structure was key to resolving the problem.
Remember, when moving sites:
- Always update both secure and unsecured base URLs in the database.
- Ensure these values match the new server’s URL structure exactly.
- Flush Magento’s cache after making changes.
If you’re still experiencing issues or need further assistance with your Magento store migration, feel free to explore our Magento services at Laravel Company for professional support.