Wordpress issue after migration - wordpress

Have changed to a new server and have migrated a number of Wordpress sites. After migration, I found that the menu links were no longer working. The content is there however.
I deleted the htaccess file and changed the permalinks to the default setting 'plain'. That worked fine. However, now my website is working with menu links like domain.com/?page_id=29.
On the old server, I have always been using the format domain.com/post_name. So I deleted the htaccess file again and changed the permalink to post_name, but clicking on the link in the menu always gives me a 404 error. Checked a phpinfo() and mod_rewrite is enabled.
The htaccess file right now is:
# 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>
How to get back working menu links when permalinks are set to postname?

On Linux, in order to relax access to the document root, you should edit the following file: /etc/httpd/conf/httpd.conf
In case you are on Ubuntu, edit the file /etc/apache2/apache2.conf (here we have an example of /var/www)
And depending on what directory level you want to relax access to, you have to change the directive
AllowOverride None
to
AllowOverride All
So, assuming you want to allow access to files on the /var/www/html directory, you should change the following lines from:
<Directory "/var/www/html">
AllowOverride None
</Directory>
to
<Directory "/var/www/html">
AllowOverride All
</Directory>

Reference Docs for you:
https://codex.wordpress.org/Using_Permalinks
I take it when you say the menu lnks stopped working again you mean to say that the link changes fine to the new permalink format of http://domain/post_name right?
If so you're issue lies in mod_redirect. Either in the .htaccess in the docroot (turning it on and having correct redirect rules) or it could in your apache web server config file. perhaps apache doesn't even have mod_redirect enabled. if it's a CPANEL server mod_redirect is likely to be enabled however.

Issue can be the path of the files as well. Do you migrate the account with same username and same path as of the old server ?
If path is different than the old server, take a mysql dump of the db, look for the path and change them to the correct new path.
If paths are same, then you could fix this issue by just resetting the permalinks i.e first change the permalinks to some other one than the current one and save and then immediately change back to old one which was in use earlier and then again save it. This have worked for me in past.

Related

Remove index.php from Wordpress URLs

I am new to Wordpress but tried all documentation and Stack Overflow posts for possible solutions but none of them have worked so far.
I have verified that mod rewrite is enabled and working as expected. Followed all steps mentioned here. https://wordpress.stackexchange.com/questions/105795/remove-index-php-from-permalinks
Also restarted Apache couple of times but still getting 404 errors when I remove index.php path from Permalink Settings.
Renamed the wordpress directory to blog to access the site at www.xyz.com/blog. Now the requirement is to access any blog posts with
www.xyz.com/blog/2018/02/09/my-wp-post without index.php in the URLs.
We don't have any other CMS content other than WP for the blog site.
Permalink settings(Custom Structure):
/index.php/%year%/%monthnum%/%day%/%postname%/
.htaccess file contents:
BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
I spent ton of time trying many different approaches answered here or elsewhere but none of them fixed my issue.
This is what I did and it has fixed my issue.
Change permalink settings to remove index.php
Update/Save .htaccess content that's at the root of the WP installation if it's not allowed to be updated automatically when we change permalink settings in WP admin panel.
This is the important step since not many Q & A mentioned this in detail, other than wordpress documentation here. Specifically check AllowOverride settings and change it to All, Apache httpd.conf will not load the .htaccess contents without this change. Of course mod_rewrite must be enabled in the server if it's not already done. In our case it's enabled by default, so didn't have to mess with this step.
Also make sure FollowSymLinks option enabled as mentioned in the WP documentation.
Last but not least, make sure you restart the Apache service/server for the changes to take effect.
Entry in httpd.conf file:
<Directory "/var/www/html/blog">
Options FollowSymLinks
AllowOverride All
Assuming Ubuntu 16.04 & Apache2
1) Activate mod_rewrite. It's available but not enabled with a clean Apache 2 installation.
sudo a2enmod rewrite
2) Restart Apache
sudo systemctl restart apache2.service
3) Setup .htaccess
(Note: Apache reccomends using a server configuration file over inserting rules into .htaccess, however, for this example, inserting rules into .htaccess is sufficient because of the negligible performance hit.)
sudo nano /etc/apache2/sites-available/000-default.conf
Insert the following in 000-default.conf
<VirtualHost *:80>
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
4) Restart Apache
Repeat step 2
5) Create .htaccess in the web root.
touch /var/www/html/.htaccess
Insert the following into .htaccess
RewriteEngine On
6 Configure the URL Rewrite (Note: Assuming index.php)
sudo nano /var/www/html/.htaccess
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
mod_rewrite is a useful Apache module that can be used effectively to ensure human-readable URLs and good SEO results. If you'd like to learn more about mod_rewrite, take a look at Apache's mod_rewrite Introduction and Apache's official documentation for mod_rewrite.
In a nutshell, as #Cnu as mentionned, the problem very few resources on the internet touch on is the fact that your apache configuration must contain an AllowOverride FileInfo directive for wordpress rewrites to work without /index.php/.
Since Apache 2.4, AllowOverride default setting is "None", which is often a roadblock in making "pretty urls" work (in this case ditching the index.php). As #cnu mentionned, you should read carefully this resource : https://wordpress.org/support/article/using-permalinks/.
Make sure to have mod_rewrite enabled
-> (to be sure create an info.php you will remove later containing the line <?php phpinfo();?>) at the root of your blog and call https://domainofyourb.log/info.php)
Make sure your .htaccess is writable by wordpress. (permission and ownership should be allowing your webserver (often with the username "apache") to edit the file.
Change your wordpress permalinks settings, and check that .htaccess file is correctly written.
Check that your apache configuration (etc/httpd/conf/httpd.conf in some linux distros) contains the directive AllowOverride FileInfo within your blog's <Directory></Directory> section
Options directive to FollowSymLinks should be the default, but if Options directive is mentionned, add FollowSymLinks for good measure.
when all that is done, don't forget to restart your Apache server. (sudo service httpd restart in my case, ymmv).
P.S : wrote this answer because I can't comment (don't have rep) on cnu's answer, and wanted to correct 1. allowoverride doesn't need to be set to ALL, it can be set to simply "FileInfo", 2. update the link to wordpress doc, and 3. provide background on the change in apache 2.4 that causes this issue.
Go to admin page, Dashboard -> Settings -> Permalink Settings -> Custom Structure, select /%postname%/ or, /%year%/%monthnum%/%day%/%postname%/ up to you.

Solving the WordPress permalink issue on a virtual server

I have a variation on the old WordPress problem regarding permalinks failing (404 errors) when permalinks are set to anything other than 'plain' (where plain = http://example.com/?p=123 and a 'pretty' URL = http://example.com/sample-post/ - taking the URL content from the article title, for example).
For the first time I am starting a site on a virtual rather than dedicated server (Hetzner hosted), where I at least was able to easily read the httpd.conf settings and other server configurations.
To recap here, the problem is that neither WordPress nor the default server settings are usually ready for the URL rewriting that allows 'pretty' URLs out of the box. When confronted with this problem before, on dedicated servers, I would ensure that AllowOverride was set to 'all' or '[directory]' and then would put in an .htaccess file into the web's document root with this traditional solver:
# 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
However this does not work on my Hetzner server. I put in the above code to an .htaccess file, set the permissions correctly and restarted. But the front page still shows links to the article lists, but the articles themselves all still throw that old 404 error.
The only access I have to the core server directives is drip-fed out in sections and radio-buttons etc via Plesk. I can't even open up the httpd settings read only to see how AllowOverride is set. Hetzner consider this a 'software problem', and therefore unsupported.
There is a section in Plesk where you can add additional Apache and Nginx directives (with duplicate boxes for http and https for Apache). I tried this in the Apache boxes and rebooted, but it didn't help (obviously this is not the actual URL I wrote):
<Directory "/var/www/vhosts/example.com/httpdocs/">
AllowOverride All
</Directory>
If anyone has any suggestions as to how I can get the usual rewrite fix in, in these circumstances, it would be much appreciated.
Okay, the problem was an obscure one, as it turned out. The virtual server was running Nginx on top of Apache, and I needed to install a plugin via Plesk which provides an 'ht access translator' for Nginx in Plesk. I pasted the rewrite directives there, they were 'translated' to Nginx-understandable commands, and the problem is solved.

Wordpress add_rewrite_rule not working

This may seem like a duplicate question, but I've read through as many SO posts with similar titles, and every one of them got much farther than I've been able to, so it seems that I must be doing something basic wrong, but I have no idea what it is. That said, here's my issue:
I'm following the steps in the 'Basic Usage' section of the add_rewrite_rule documentation page, making sure to flush and regenerate rewrite rules.
I've got a page with an id of 68, and I want to redirect this:
http://localhost.test.wordpress/leaf/68
to this:
http://localhost.test.wordpress/index.php?page_id=68
I have verified that the latter does exist, and that I can navigate to it directly.
So in my theme's functions.php file, I wrote the following:
function custom_rewrite_basic() {
add_rewrite_rule('^leaf/([0-9]+)/?', 'index.php?page_id=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_basic');
And then I went to the Settings -> Permalinks section of my WP admin panel and hit 'Save Changes' with no changes, as the docs page directs me to do.
The problem I'm running into is that when I navigate to http://localhost.test.wordpress/leaf/68, I get a 404 error. Clearly, the docs indicate that this should not be happening, and none of the other SO posts I've seen have had this problem (they get past this step and run into difficulty in some other aspect of the redirect).
The redirect code I put in functions.php is an exact copy/paste from the docs page, and yet because of the 404, I'm led to believe that no WP code is being run at all. Can you let me know what I'm doing wrong? I'm completely new to WP.
If the site doesn't seem to be respecting changes you make to the .htaccess file, you should make sure that the <VirtualHost> configuration explicitly gives the .htaccess files permission to override certain configurations.
In your <VirtualHost> entry there should be a <Directory> entry as well. You'll need to add the AllowOverride All command to it, like this:
<Directory /path/to/your/web/document/root>
AllowOverride All
Require all granted
</Directory>
Also, if the site isn't respecting your Rewrite commands, you should make sure to check your httpd.conf to see if the mod_rewrite module is loaded. Look for a line similar to this:
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
If it is commented out with a #, uncomment that line (remove the #) and then restart your apache.
Based on your supplemental answers in the comments, your .htaccess doesn't sound like it's setup properly (getting server 404 page instead of WP 404). Add the following to your .htaccess file:
# 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
This redirects (almost) every request to index.php, to be processed by WP internally. This needs to be set up properly for your rewrite rules to be processed by WP.

postname giving URL was not found

I have an issue by getting permalink to work on debian8.
My .htaccess looks like this:
# 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
and permission 666.
But then I change my permalink to postname instead of plain and I get:
requested URL was not found on this server
on all of my URLs except the start page.
WordPress uses Apache's "mod_rewrite" to serve pages and posts using permalinks, and it sounds like that module is not enabled on your server. To enable it, type sudo a2enmod rewrite, and then restart Apache. After that, permalinks should work.
You can also set any file permissions that you changed back to what they were originally.
Edit:
In your Apache config, you might also need to change the AllowOverride setting. The default on Ubuntu does not allow all .htaccess directives to work, so it's likely the same on Debian. You can use AllowOverride All or choose specific options instead. FileInfo is the one you will need for mod_rewrite. Apache's docs (2.4) for AllowOverride are here: https://httpd.apache.org/docs/2.4/mod/core.html#allowoverride
I solved my problem by using the below command
sudo a2enmod rewrite

Permalinks in Wordpress - Page not found

I've been searching for hours but haven't found anything that seems to be able to solves this issue.
Here's the scenario:
I'm making a wp theme based on the "Twenty Eleven" theme. Everything went fine til I decided to change the urls to permalinks. The only page being displayed is the static page that I have defined earlier.
I have set up the htacces file. In fact, WP did it automatically. Everything works if I switch back to the default setting, but, for SEO, I would rather use the permalinks option.
Here is my htaccess file (it is on my WP installation folder):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /mysite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mysite/index.php [L]
</IfModule>
# END WordPress
I have seen this post wordpress .htaccess with permalinks but nothing there could help me. Any help would be very nice.
UPDATE : Things I have tried already:
Delete pages and create again.
Access the permalink field on wp_options (db) and setting the value to blank and set the permalink option in the admin again.
I´m running it on windows 7 through an apache2 installation of Zend Server.
I thought it was a problem related to my localhost environment, so I put the site online. No luck at all. I'm assuming that wordpress can´t change permalinks to a more friendly url type when you set a static front page. What a shame.
For those using apache. You will need to
Ensure you have .htaccess in root path of the site you are hosting. Example /var/www
Update the /etc/apache2/sites-available/default
From
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
To
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Hope this helps someone
If editing apache2's default configuration and Wordpress' .htaccess files don't help the reason could simply be that apache2's rewrite module is not enabled. This is usually the case for those who install apache2 themselves. Wordpress needs apache2's rewrite module enabled to support permalink edits. If, like me, you'd rather not edit conf files by hand, apache2's rewrite module can be enabled by running these commands as root (I'm using Ubuntu 18.04, so these commands might be different in distros that aren't based on Ubuntu or Debian):
a2enmod rewrite
and restart the apache2 service by running:
systemctl restart apache2
or
service apache2 restart
If you're running Ubuntu 18.04, you can check out all available apache2 modules under /etc/apache2/mods-available and see what modules are enabled by listing the files under /etc/apache2/mods-enabled.
I don't know if you have found the solution to it, but I solved this problem by simply turning on LoadModule rewrite_module modules/mod_rewrite.so in httpd.conf file.
Also see Permalinks on WordPress (Amazon EC2)
I had the same problem, but the author in the above link suggested to do three things (it worked for me!):
Go to /etc/httpd/conf and edit httpd.conf
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
Change also AllowOverride if it is set to None
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride All
I you haven’t created it yet, place in the root directory of your wordpress
installation a .htaccess file with the following contents:
# 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
There are two 'Directory' sections in the httpd.conf file.
One is for root folder and another for htdocs folder.
You must edit both fields to make WordPress pages work again.
Hope it helps.
This worked for me like #Skillachie wrote BUT be also sure to include those settings in the 000-default-SSL.conf file if you use SSL!
<Directory /var/www/PATH>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>

Resources