Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove trailing slashes

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...

like image 732
Erik Avatar asked Jun 30 '09 14:06

Erik


People also ask

How do I get rid of trailing slash in Excel?

We can remove a trailing slash from a URL LINK by using a combination of LEFT, RIGHT, and LEN functions.

How do I get rid of trailing slashes in Python?

The most common approach is to use the . rstrip() string method which will remove all consecutive trailing slashes at the end of a string.

How do I get rid of trailing slash in WordPress?

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.


1 Answers

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.

like image 146
swedish boy Avatar answered Oct 02 '22 23:10

swedish boy