Search code examples
wordpressapache.htaccessxampppermalinks

Wordpress permalinks no longer work after site migration


After a site migration, the home page of new site url loads, but the permalinks all redirect to Apache's(i'm on XAMPP stack) default page:

Object not found!

The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.

If you think this is a server error, please contact the webmaster.

Error 404

localhost
Apache/2.4.16 (Win32) OpenSSL/1.0.1p PHP/5.5.28 

I don't think this is wordpress db issue. otherwise, I would've gotten wordpress's default page not found page. What else is causing this? Is there a .htaccess file somewhere that needs to be modified?

EDIT I used this script for the db migration: https://interconnectit.com/products/search-and-replace-for-wordpress-databases/

EDIT So added the default .htaccess from https://codex.wordpress.org/htaccess, and it redirects to xampp's homepage. So that officially means the permalink structure somewhere in wordpress db/routing is broken(not routing properly). My new site url is "localhost/somewebsite", where "localhost" is default domain name of my xampp/local dns setup.


Solution

  • I found a solution. You have to make a virtual host for your site. Here are the 4 steps you have to do:

    1. Go to C:\xampp\apache\conf\extra\httpd-vhosts.conf and make two new hosts. First set up a localhost so that when you want to call another folder which is no virtual host work:

    #localhost
    <VirtualHost *:80>
      DocumentRoot "C:/XAMPP/htdocs"
      ServerName localhost
    </VirtualHost> 
    #My Website
    <VirtualHost *:80>
      DocumentRoot "C:/XAMPP/htdocs/my-website" #your path to your installation
      ServerName my-website.dev #Change my-website to your wish name/url
      ServerAlias my-website.dev #Change my-website to your wish name/url
    </VirtualHost>   

    1. Go to the Windows hosts file and add this:

    	127.0.0.1		my-website.dev

    1. Go to your Wordpress .htaccess file and paste this into it:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress

    You have to save all files and restart your Apache and MySQL Server

    1. Now you have to change all Worpdress url's in your database to http://my-website.dev (its a sample! You've to paste the url you set in the xampp host file here). To make this step easy you've to download the Database Search and Replace Script in PHP from https://interconnectit.com/products/search-and-replace-for-wordpress-databases/. Unzip this and paste the folder Search-Replace-DB-master in C:/XAMPP/htdocs. Open your browser and go to this url: http://localhost/Search-Replace-DB-master/. Now enter your database informations in the database section (db-name, user, passwort). Go to the top and enter in the replace field your old url (in my situation: https://my-website-old-url.com) and in the field after with your new url (example: http://my-website.dev).

    Now click on live run in the middle of the page and all of your old urls will replaced with the new one.

    It may be that you have to set AllowOverride to All in your httpd-conf file. You also have to check that LoadModule rewrite_module modules/mod_rewrite.so in your httpd-conf has no # infront of it because of rewriting etc...

    Finaly go to the browser and enter your new url: my-website.dev

    Thats it! Hope I can help you with this.