johngodley / search-regex

Search Regex adds a powerful set of search and replace functions to WordPress that go beyond the standard searching capabilities, with full regex support.
https://searchregex.com
GNU General Public License v3.0
32 stars 9 forks source link

redirect brakes plugin #171

Closed kocjs closed 8 months ago

kocjs commented 1 year ago

I have redirect to remove trailing slash on the end of file in .htaccess, prevents the plugin form working. I would expect the plugin to work anyway.

johngodley commented 1 year ago

You will need to provide more information. Break the plugin how?

kocjs commented 1 year ago

Sure, sorry. I am unable to upload a screen shot. It says:

Your wordpress rest API is returning a 404 page. This is almost certainly  an external plugin or server configuration issue. 

You will will need to fix this on your site. Redirection is not causing this error.

However it is definitely the redirection, as if i revert it, it works as expected. This is how i set up the redirect.

<IfModule mod_rewrite.c>
#Turn on Rewrite Engine
RewriteEngine On

#Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://example.com/%1 [R=301,L]

#Include trailing slash on directory 
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://example.com/$1/ [R=301,L]

#Force HTTPS and remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off  
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
</IfModule>

Can you access your REST API without it redirecting? Not sure if it is without redirecting, put when i click i do not get a 404 error but the json file. The link looks like this. https://example.com/wp-json

kocjs commented 1 year ago

Ok, i have managed to update my rules. And it is working now. This is all new to me. It looks like my redirection did prevent your plugin to access the access the rest api.

Might it be that the error message Redirection is not causing this error. is referring to your other plugin?

#Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/wp-json/ [NC]
RewriteRule ^ https://example.com/%1 [R=301,L]

#Include trailing slash on directory 
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/wp-json/ [NC]
RewriteRule ^(.+)$ https://example.com/$1/ [R=301,L]

I noticed that you are the author of the redirection plugin, so it seems like you specialize in redirecting links. Are the updated rules for removing the trailing slash at the end of the link okay, or would you suggest a different approach? I especially wonder, because visiting my site form http://www.example.com/ still gives me two redirects, and i am looking for a rule set that just causes one redirect.

I have another question that I would like to ask you. Many SEO posts suggest that it is not important, yet they all have a trailing slash in that post link. I understand that when I visit a post link, I am requesting a file. While it may not be important anymore, I still wonder how the practice of adding trailing slashes to the end of a link started in the first place. I tried to find out about the origin of the issues but i was not able to.

I am going to close this, as it works for me.

kocjs commented 1 year ago

I am opening this again, just in the hope to get answers on this questions. Feel free to close it.

johngodley commented 1 year ago

WordPress, by default, will add a trailing slash. It makes no real difference whether you have one or not, so I wouldn't worry about it and just go with the default.