How Can Multiple Trailing Slashes Can Be Removed from a Url in Ruby

How can multiple trailing slashes can be removed from a URL in Ruby

If you just need to remove all slashes from the end of the url string then you can try the following regex:

"http://emy.dod.com/kaskaa/dkaiad/amaa//////////".sub(/(\/)+$/,'')
"http://www.example.com/".sub(/(\/)+$/,'')

/(\/)+$/ - this regex finds one or more slashes at the end of the string. Then we replace this match with empty string.

Hope this helps.

How to remove random excess of slashes from url?

Use regular expressions:

require 'uri'
url = URI.parse('https://domain.com/////url/url2')
url.path.gsub! %r{/+}, '/'
p url.to_s

How to remove a URL's trailing slash in a Rails app? (in a SEO view)

I'd use Apache's mod_rewrite. Try this:

RewriteEngine on
RewriteRule ^(.+)/$ $1 [R=301,L]

EDIT: Added R=301. I'm guessing there is an SEO advantage to that vs. the default 302.

Remove trailing '/' from rails string

Try:

var apis = '<%= @event.slink.split('-')[-1].gsub(/\/$/, '') %>';

Ruby: How to remove trailing backslashes from a string?

See following code:

1.9.3p125 :022 > s = "cat\\"
=> "cat\\"
1.9.3p125 :023 > puts s
cat\
=> nil
1.9.3p125 :024 > s.chomp("\\")
=> "cat"
1.9.3p125 :025 >

How to make Rails do not ignore trailing slashes in the routes?

I'm not sure why you need your routes to understand that there is a trailing slash. It doesn't matter if you put the trailing slash to Rails. It should respond either way.

If you want all of your links to have a trailing slash, you can put the following in your application.rb:

config.action_controller.default_url_options = { :trailing_slash => true }

This will make all links generated by Rails have a trailing slash. Now, if you're trying to reject any route that does not contain a trailing slash, then I'm not sure about how to do that.

how can i remove trailing slashes form the url

To remove a trailing slash using .htaccess you can use:

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


Related Topics



Leave a reply



Submit