Home > OS >  Wordpress page PHP snippet to fetch all requests to subdirectories
Wordpress page PHP snippet to fetch all requests to subdirectories

Time:06-27

I am developing a PHP snippet for a Wordpress website page. Imagine this snippet is at a page located at

wp-example.com/abc

But when I call

wp-example.com/abc/def

Wordpress assumes it is a totally different page and says page does not exist, and my snippet is not applied.

In my PHP snippet, located at wp-example.com/abc, how can I fetch all the requests to subdirectories example.com/abc/**?

edit: I don't want to amend my web server configuration, I want to do that purely within Wordpress

CodePudding user response:

You can solve this by addding to .htaccess file 2 lines: (not tested)

RewriteEngine on
RewriteRule ^/?abc/(.*)$ /abc [R,L]

CodePudding user response:

Use this .htaccess code.

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

CodePudding user response:

Step 1: Allow Multisite

To enable the Network Setup menu item, you must first define multisite in the wp-config.php file.

Open up wp-config.php and add this line above where it says That's all, stop editing! Happy blogging.. If it doesn’t say that anywhere, then add the line somewhere above the first line that begins with require or include:

/* Multisite */
define( 'WP_ALLOW_MULTISITE', true );

Step 2: Installing a Network

The previous step enables the Network Setup item in your Tools menu. Use that menu item to go to the Create a Network of WordPress Sites screen.

To see an example of the Create a Network of WordPress Sites screen, look at Administration >Tools > Network Setup. The screen does not look exactly the same in all circumstances. The example shown is for an installation on localhost, which restricts the options available. https://prnt.sc/28ZPHhke7ZuQ

Step 3: Enabling the Network

To enable your network, follow the instructions on the Create a Network of WordPress Sites screen. The instructions that you see are customized for your installation. They might not be the same as the examples you see here. https://prnt.sc/XBGn0dUbWMJR

Add the first code snippet to your wp-config.php directly above the line The snippet looks like this, but adapted to your own site:

// Example code but it is not the code of your website
define('MULTISITE', true);
define('SUBDOMAIN_INSTALL', true);
define('DOMAIN_CURRENT_SITE', 'My Website');
define('PATH_CURRENT_SITE', '/');
define('SITE_ID_CURRENT_SITE', 1);
define('BLOG_ID_CURRENT_SITE', 1);

Add the second code snippet to the .htaccess file and replace other WordPress rules.

// Example code but it is not the code of your website key
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-] /)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-] /)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-] /)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
  • Related