Showing results for 
Search instead for 
Did you mean: 

How can I remove trailing slashes in a URL?


How can I remove trailing slashes in a URL?

I use the following rewrite script on my Apache servers to remove trailing slashes in URLs:

RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]

I now need to change this so it works with Stingray - can anyone help?

Frequent Contributor

Re: How can I remove trailing slashes in a URL?

There are many ways to remove trailing slashes, you could use a regular expression match similar to your rewrite rule, but I would probably avoid that and do something like this instead:

# Read in the path

$path = http.getPath();

# if it's the root dir exit the script

if( $path =="/" ) break;

# if we end with a slash, then redirect; this does a 302 redirect

if( string.endswith( $path,"/" )) http.redirect( string.drop( $path,1 ));

This script uses http.redirect() which issues a 302 redirect, if you want it to be a 301, then you could switch the last block with the one below. The version below uses the more long winded http.sendResponse(), but this function allows you to send back any HTTP resopnse you wish, not only redirects.

# Issue a 301 permanent redirect

if( string.endswith( $path,"/" )){

   $location = string.drop( $path,1 )

   http.sendResponse( "301 Moved","text/plain","Object moved to: ". $location,"Location: ". $location );