301 Redirect for index.php in WordPress URLs

Содержание:

Hello! I was tasked by SEO specialist to remove index.php from the URLs on a WordPress site.

Usually, this can be handled via .htaccess, but in this case I ran into a couple of nuances:

  1. We need to distinguish the request type – regular GET requests versus AJAX/REST calls.
  2. Is the request targeting the public-facing site or the admin area?
  3. If the site uses a caching plugin – specifically WP Super Cache in «expert» mode – the .htaccess rules may not take effect.

After struggling to combine various rules, I decided to write a lightweight MU plugin. However, because WP Super Cache doesn’t allow MU plugins to run unless «Late Initialization» is enabled, I ended up bundling the same logic as a mini-plugin for WPSC. You can check out the final result in the GitHub repo, and I’m also including the archive of the current 1.2.1 release:

Functionality

  1. Checks the request type – only acts on GET requests.
  2. Ignores calls to WordPress paths (those containing “wp-”), including the admin area.
  3. Allows you to choose whether to add or remove a trailing slash via the $remove_trailing_slash option.
  4. If all conditions are met, issues a 301 redirect to the URL without index.php.
  5. When triggered, adds an HTTP header: X-HIDDEN-MU-INDEXPHPREDIR: active or X-HIDDEN-WPSCPlugin-INDEXPHPREDIR: active

Installation

  1. Unpack the plugin into the root of your site (or into the subdirectory containing wp-content).
  2. Activate the plugin named “WP Super Cache – index.php redirect fix.”
Включение подплагина для WP Super Cache
Enabling sub-plugin of WP Super Cache
MU-плагин активен
MU-plugin is enabled by default
Результат работы плагина - index.php удален
Result – index.php is removed

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top