Another way to get rid of the trailing slash `/` in URL?

Is there another way to get rid of the trailing slash /

There is this .htaccess rule:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]

This one didn’t work either:

To complement Jon Lin’s answer, here is a no-trailing-slash technique that also works if the website is located in a directory (like example.org/blog/):

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]

from http://stackoverflow.com
both don’t work.

Why do you want to remove the trailing slash?

It’s common for URLs with a trailing slash to indicate a directory, and those without a trailing slash to denote a file. URL’s without a trailing slash or an extension just don’t look right.

In the case of tidy links the addresses end up at the same location. For example The Best Mac Website Builder - RapidWeaver Classic and The Best Mac Website Builder - RapidWeaver Classic will both go to the same URL. The Web server knows that even a technically incomplete address (a URL that ends with a folder name only) needs the trailing slash. Click on The Best Mac Website Builder - RapidWeaver Classic and then look at the address bar. The trailing slash has been added.

Stackoverflow is probably the largest technical forum on the internet without a link to the post you are using it’s hard for anyone else to see what the author(s) might be talking about.

I think this is the post you are talking about:
php - Htaccess: add/remove trailing slash from URL - Stack Overflow

Both solutions offered above do work. They both will remove the trailing slash from a URL. The problem is when you use tidy links you aren’t providing a complete URI, you aren’t giving the filename portion of the URL. So when either of the above options executes, they will remove the trailing slash.

So in my example URL above
https://www.realmacsoftware.com/rapidweaver/
Will become:

https://www.realmacsoftware.com/rapidweaver

Next, Apache (or Nginx) will then get a URL that doesn’t end with a / indicating it’s a directory (folder) and it’s not a file name. It could issue a 404 (not found) but it also knows that it has a directory with that name. So a 301 redirect will get issued. Back to:

https://www.realmacsoftware.com/rapidweaver/

Now the webserver has a directory name, It knows that if it has a request for a directory without a file name to look for files with certain names that are in that directory.

A typical Apache DirectoryIndex Directive:

DirectoryIndex index.html index.php index.cgi

The Webserver will then look from left to right for a file in that directory until it finds one that matches the name or it will then issue a 404.

So both solutions given above do strip off the trailing slash. You can paste either one of them into https://htaccess.madewithlove.be/ an excellent htaccess file tester. (the first one you’ll need to remove the comments as # isn’t at the beginning of a line). And you can see that the Output URL is returned without the trailing /.

But as you have discovered the webserver adds it back in.

4 Likes

Alright, I see your (the) point.

‚ and probably I’ll have to settle with the hard truth of internet web servers.

(and I checked it with madewithlove.be and yes the slash was added back again.)


But there are plenty of pages out there, which don’t finish with a / slash, f ex:

www.kurser.se/kurs/dans

/

with best regards, Omar KN, Stockholm, Sweden

in this example the web servers is Microsoft-IIS/10.0 and the file name is actually dans.

BTW: IIS (aka Windows Server) is a totally different beast.

3 Likes

A related issue :slightly_smiling_face:

When scanning for broken links etc. there are quite a few links with double // - like this:

https://www.livingislam.org//humankind/man-and-woman/

https://www.livingislam.org//english.html

What is the cause of this double slash?

/

with best regards, Omar KN, Stockholm, Sweden

That’s standard operating business for web links. It’s rare that you don’t see this! For example the link to this particular thread is:

http://forums.realmacsoftware.com/t/another-way-to-get-rid-of-the-trailing-slash-in-url/34944/5

I would not call this a “related issue”. It’s how the web works.

Not sure what you are using to scan, but both links above work, so they aren’t broken.

The URL ( Uniform Resource Locators) standards were defined in RFC 1738). Every HTTP URL conforms to the syntax of a generic URI. The URI consists of

scheme:[//authority]path[?query][#fragment]

The double slashes you see in the example URL’s above would be considered part of the path. The path is made up of a sequence of path segments separated by a slash ( / ).

A segment may also be empty, resulting in two consecutive slashes ( // ) in the path component.

Integrity app by PeacockMedia

Right they aren’t broken, but it does look a bit unusual (with 2 slashes).

A segment may also be empty, resulting in two consecutive slashes ( // ) in the path component.

Maybe this is the reason?!

/okn

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.