2

I have wordpress installed on a root directory. Wordpress was often so slow, I created a node script which will look at all pages on the website, and a create a static directory for each one with an index.html file.

Before making them static, the directory looks like

/wp-content
/wp-includes
/etc etc etc

After

/wp-content
/wp-includes
/etc etc etc
/page-with-same-url-from-wordpress
   /index.html

If I then visit mysite.com/page-with-same-url-from-wordpress/, I am served up the static page. Great, and it loads quick.

The issue is that when I go back to edit the page on wordpress, and want to preview any edits, I still see the overriding index.html content instead of the actual wordpress php content. I see why, but as a workaround, when I want to edit a file, I now need to delete the /page-with-same-url-from-wordpress/ dir which will allow me to see the wordpress changes in preview mode, and when I am done, I run the node script and it creates the /page-with-same-url-from-wordpress/ dir again.

Is there any type of rewrite rule I could add to the .htaccess file that would allow the server to always serve the index.html file UNLESS I am viewing the url in preview mode (which looks like mysite.com/page-with-same-url-from-wordpress/?preview_id=2313

I really appreciate any insight. Thank you.

Relevant part from .htaccess:

# 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
5
  • Yes, this is possible, but it will mean changing your existing rules - please post the contents of your .htaccess file.
    – MrWhite
    Commented Jan 24, 2022 at 21:28
  • In fact, it would be preferable if you create the static site in an alternative subdirectory (eg. /static) - this does not need to be in the visible URL. But this would mean you could just add an additional rule to the existing .htaccess file and not have to change the standard WordPress code block (which is against recommendations).
    – MrWhite
    Commented Jan 24, 2022 at 21:37
  • @MrWhite Thank you for the reply. I considered a seperate subdirectory but with some strange edge cases and things I wanted to avoid, I decided to go with it in the same directory which I know also has some drawbacks. Here is the htaccess file contents - pastebin.com/KReDs4ws Really appreciate it.
    – Sean
    Commented Jan 24, 2022 at 22:04
  • Aside: Do you have a static homepage as well?
    – MrWhite
    Commented Jan 25, 2022 at 1:04
  • Yes. I have the standard index.php file in the root dir, but have added the static index.html which is overwriting it. I am attempting your solution now.
    – Sean
    Commented Jan 25, 2022 at 1:05

1 Answer 1

2

You could force any URL that contains a preview_id URL parameter to the WordPress front-controller (ie. index.php). This will effectively bypass the WP code block for these type of URLs. (It is the WP code block that skips these requests when they map to your static directories.)

(This avoids having to modify the existing "WordPress" rules, as I mentioned in comments.)

Add the following before the WordPress code block, ie. before the # BEGIN WordPress comment marker.

# Send any URL with "?preview_id=" immediately to the WP front-controller
RewriteCond %{QUERY_STRING} ^preview_id=
RewriteRule ^ index.php [L]

# BEGIN WordPress
: Remaining directives follow...

UPDATE:

The wordpress theme I'm using has a live builder feature where you can visually see your changes as you make them.

Add a series of OR'd conditions for each request that needs to be routed through WordPress. For example:

# Force some URLs to the WP front-controller
RewriteCond %{QUERY_STRING} ^preview_id= [OR]
RewriteCond %{QUERY_STRING} ^fb-edit= [OR]
RewriteCond %{QUERY_STRING} ^builder=
RewriteRule ^ index.php [L]

NB: Never use the OR flag on the last condition since that will unconditionally send every request to index.php. (It would basically be interpreted as OR true.)

Alternatively, that can be written with one condition using regex alternation if there are only a few exceptions like this. For example:

# Force some URLs to the WP front-controller
RewriteCond %{QUERY_STRING} ^(preview_id|fb-edit|builder)=
RewriteRule ^ index.php [L]
6
  • 2
    Worked like a charm. I can now see my previewed content instead of the static directory! This is also working for the static homepage index.html file as well. Thank you! That saved me many hours of trying to wrestle syntax I am not so familiar with.
    – Sean
    Commented Jan 25, 2022 at 1:13
  • There's one more issue I'm facing, tried my best to solve it but could not. The wordpress theme I'm using has a live builder feature where you can visually see your changes as you make them. The url is=t uses in this mode is mywebsite/page-name/?fb-edit=1. I tried adding RewriteCond %{QUERY_STRING} ^fb-edit= as a rewrite condition, but it only partially works. The live page builder will start to load, but then gets stuck and doesn't fully load. If you have any possible solutions, that would be amazing. Thank you.
    – Sean
    Commented Jan 27, 2022 at 16:41
  • 1
    @Sean You will certainly need the OR flag on the first condition. Have you checked the "network" tab in the browser dev tools to check all the requests that are being made? There maybe requests for other resources that are being blocked?
    – MrWhite
    Commented Jan 27, 2022 at 16:47
  • Wow. Great tip. I checked the network tab before asking this, but wasn't sure if I was shooting in the dark so stopped investigating there too early. It appears along with mywebsite/page-name/?fb-edit=1 it also makes a request to mywebsite/page-name/?builder=...etc So I added {QUERY_STRING} ^fbuilder= as another condition, and the page builder loaded! Thank you! Lastly, I now have three separate conditions and should instead use the OR command as you mentioned to only have one. I'm sure I can get the correct syntax after some googling but will let you know if I fall short.
    – Sean
    Commented Jan 27, 2022 at 17:01
  • 1
    @Sean Yes, that's correct. (I added that to my answer.)
    – MrWhite
    Commented Jan 27, 2022 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.