admin管理员组

文章数量:1122832

It seems that Wordpress has changed the default .htaccess file so that you can no longer run different PHP code within the a subfolder. It also states that the block in the .htaccess file is dynamically created, so messing with it directly would be problematic.

So what is the correct way to support running other PHP code that isn't related to Wordpress in a subfolder that Wordpress is installed?

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
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]
</IfModule>

# END WordPress

It seems that Wordpress has changed the default .htaccess file so that you can no longer run different PHP code within the a subfolder. It also states that the block in the .htaccess file is dynamically created, so messing with it directly would be problematic.

So what is the correct way to support running other PHP code that isn't related to Wordpress in a subfolder that Wordpress is installed?

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
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]
</IfModule>

# END WordPress
Share Improve this question edited Jun 8, 2024 at 21:44 Exit asked Jun 8, 2024 at 17:21 ExitExit 3194 silver badges12 bronze badges 4
  • "It seems that Wordpress has changed the default .htaccess file so that you can no longer run different PHP code within the same folder." At least for me, this doesn't seem to be the case. For example, I can have a foo.php in the folder next to the .htaccess and navigate to it via /foo.php and it will run. – Wongjn Commented Jun 8, 2024 at 18:14
  • I should note, this is for a subfolder. I will update the question. – Exit Commented Jun 8, 2024 at 21:44
  • "It seems that Wordpress has changed the default .htaccess file so that you can no longer run different PHP code within the (sic) a subfolder." At least for me, this doesn't seem to be the case. For example, I can have a foo.php in some arbitrary subfolder and navigate to it via /some/arbitrary/folder/foo.php and it will run. – Wongjn Commented Jun 8, 2024 at 21:54
  • It isn't the case for me, so I'm not sure what it is. – Exit Commented Jun 11, 2024 at 18:42
Add a comment  | 

1 Answer 1

Reset to default 0

To run non-WordPress PHP files in a subfolder alongside a WordPress installation, follow these steps to modify your .htaccess file:

  1. Choose a Subfolder Name: Decide the name for your subfolder, e.g., mycustomphp.

  2. Modify the .htaccess File: Add the following lines above the # BEGIN WordPress section to exclude your subfolder from WordPress's URL rewriting:

    RewriteEngine On
    RewriteRule ^mycustomphp/ - [L]
    

    Ensure the entire .htaccess looks like this:

    RewriteEngine On
    RewriteRule ^mycustomphp/ - [L]
    
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    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]
    </IfModule>
    # END WordPress
    

This approach allows your PHP files to operate separately from the WordPress environment without altering core rewrite rules that could affect your main site.

本文标签: redirectRunning nonWordpress PHP files