I'd like to work with pages without trailing slashes. So now I want my URL's with an trailing slash to redirect (using .htaccess) to the same URL without the trailing slash.
I got two .htaccess files:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule (.*) public/$1
</IfModule>
And one in my public folder:
DirectoryIndex index.html index.php
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
I tried adding the following rule to the .htaccess file in the public folder:
RewriteRule (.*)/$ $1 [R,L]
But then: example.com/public/page/view/2/
Redirects to: example.com/**D:/webserver/**public/page/view/2
Which is obviously not what I want...
We can remove a trailing slash from a URL LINK by using a combination of LEFT, RIGHT, and LEN functions.
The most common approach is to use the . rstrip() string method which will remove all consecutive trailing slashes at the end of a string.
To do so, go to the “Settings -> Permalinks” section and, depending on your situation, either add or delete the last slash. The “Custom Structure” field ends with a slash, so all other WordPress URLs will have the trailing slash.
Wordpress redirects depending on what your permalink structure is. So any changes to .htaccess helps little since Wordpress will add/remove them for you and peform a wp_redirect() after .htaccess done things. In worst case you end up with a redirect loop.
A solution is to turn off Wordpress redirecting using.
// perhaps an if(is_something()) before here ...
add_filter('redirect_canonical', '__return_false');
You should wrap this inside an if statement or such where you perform a check for a certain page/directory/etc. Putting the above directly in your functions file will turn off wordpress redirections and probably break things.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With