Problems about placeholders of Netlify redirects - url-rewriting

I am trying to set up some URL rewrites in Netlify.
I want to set up a rewrite to redirect
https://example.com/blog/2019/05/15/hello.html
to
https://example.com/blog/2019/05/hello.html
I read the document - https://www.netlify.com/docs/redirects/#placeholders:
You can use placeholders in the origin and target paths:
/news/:year/:month/:date/:slug /blog/:year/:month/:date/:slug
This would redirect a URL like /news/2004/02/12/my-story to /blog/2004/02/12/my-story
And I have my _redirects file like this:
# Redirect old permalinks to new format
/blog/:year/:month/:date/:slug.html /blog/:year/:month/:slug.html 301!
It did work to redirect
https://example.com/blog/2019/05/15/hello.html
to
https://example.com/blog/2019/05/hello.html
But there is a weird problem, it also redirects
https://example.com/blog/2019/05/not-a-date/index.html
to
https://example.com/blog/2019/05/index.html
My question is, Is it able to (if yes, how can I) make the :date placeholder matching day only? not-a-date is not a date obviously, shouldn't :date match 01 to 31 only?

To my knowledge the placeholders aren't typed (allowing only numbers). So anything will match and it's not possible to define only days as a pattern to match.

https://www.healingislands.com/* http://www.healingisland.com:splat 301!
putting { /* } this at the end of the first link and putting { :splat 301! } this at the end just worked for me

Related

Web API - pass double parameters to GET method

I have the following method:
[Route("GetEditorialRequestsByCoordinates/{lat:min(-90):max(90)}/{lng:min(-180):max(180)}")]
[AutomapperExceptionApiFilterAttribute]
public HttpResponseMessage GetEditorialRequestsByCoordinates(double lat, double lng)
{
}
It works fine when I call...
GET /v1/api/request/GetEditorialRequestsByCoordinates/48/2
But I want to pass double value like this:
GET /v1/api/request/GetEditorialRequestsByCoordinates/48.999/2.777
I got an error (404 not found). Seems, it can't find appropriate method by route.
I have tried to set route by way:
[Route("GetEditorialRequestsByCoordinates/{lat:double:min(-90):max(90)}/{lng:double:min(-180):max(180)}")]
...but it does not work either.
How can I fix it?
Simply adding a / to the end of the URL corrected it for me. Looks like the routing engine is viewing it as a 2.777 file extension, rather than an input parameter.
Additionally, it looks like you can register a custom route that automatically adds a trailing slash to the end of the URL when using the built-in helpers to generate the link.
he easiest solution is to just add the following line to your RouteCollection. Not sure how you would do it with the attribute, but in your RouteConfig, you just add this:
routes.AppendTrailingSlash = true;
For more details check here.
last of all max and min function works for integer
max: Matches an integer with a maximum value. {x:max(10)}
I think it does not work for double.

Rails 4 custom path helper

I would like to customize my path helpers in Rails 4.
I have a Site5 website (which uses Apache server) with multiple subdomains. I have rewrite rules in my .htaccess file which adds the folder name to the url. For example
cs1337.mysite.com/login
is rewritten to
cs1337.mysite.com/cs1337/login
In routes.rb, I have added a scope in routes.rb:
scope '/cs1337' do
get '/login', to: 'sessions#new', as: :login
# etc.
end
which gives me the correct routing. Everything works, but the annoying thing is that all my path helpers have the '/cs1337' prefix, which is unnecessary since the .htaccess rewrite rules add it back in. For example,
login_path # => /cs1337/login
when only
login_path # => /login
is necessary.
I would like to override all of the path helpers to strip off the prefix, using something like
def <override all path helpers>
path = super
path.gsub(%r{^/cs1337}, '')
end
I know the path helpers are dynamically generated, but I can't figure out where start ... I can't even find what module the path helpers are generated in.
Thanks for any help you can offer!
Short answer: No, as far as I can tell.
I couldn't find anything that lets you have the route Rails intercepts be different from the path that the helpers give you. I don't think Rails plays well with .htaccess rewrites.
Do you need the folder name to be in the path? If not, I would remove the scope '/cs1337' do and use something like this StackOverflow post to do domain-specific routes.
If you need it there, I would recommend being okay with the end user seeing "cs1337.mysite.com/cs1337/path".

How do I route based on a url parameter in sinatra?

I am using Sinatra and I want to use something like a referrer code in my urls that will somewhat control access and identify the provenance of a given URL.
/secret-code/rest/of/path
should be rejected if "secret-code" is not in a predetermined list.
I want to use route conditions
set(:valid_tag) { |tag| condition { tag === 'abcd' } }
get '/:tag', :valid_tag => params[:tag] do
'Hello world!'
end
but params is not in scope. Do I need to dispatch in the block? What is the best way to handle multiple routes without having to duplicate the tag checking logic in each one?
/secret/route1/
/secret/route1/blah
/secret/route2/
Is there a way to chain handlers? Can I do
get /:tag/*
# check :tag
redirect_to_handler(params[:splat])
By the sounds of things it looks like you're trying to make use of Sinatra's named parameters. Params is only in scope within the block:
get '/:secret_code/*' do
redirect_to_handler unless secret_codes.include? params[:secret_code]
end
The code above assumes you have a collection of 'secret_codes' that you're going to check with the secret_code from the URL.
(Answering my own question)
Sinatra matches the lexically first rule and you can pass onto the next matching rule using 'pass'. So something like this works as long as it is the first rule that would match.
get '/:tag/*' do
halt_if_bad_tag params[:tag]
pass
end
get '/:tag/route1' do
'hello world'
end

How can I create a Rails 3 route that will match all requests and direct to one resource / page?

I have a rails app (Rails 3.0) that I need to temporarily take out of service. While this is in effect, I want to create a new route that will direct all requests to a single piece of static content. I have a controller set up to serve my static pages.
I tried something like this:
match '*' => 'content#holding'
and
match '*/*' => 'content#holding'
to match a wildcard route as described here:Rails 3 route globbing without success.
This is probably a really simple answer, but I couldn't figure it out.
/EDIT/
Forgot to mention that I did have this rule at the very top of my routes.rb file.
Rails needs to bind the url parameters to a variable, try this:
match '*foo' => 'content#holding'
If you also want to match /, use parenthesis to specify that foo is optional:
match '(*foo)' => 'content#holding'
I did this just yesterday and first came up with the solution that klochner shows.
What I didn't like about this is the fact that whatever you enter in the URL, stays there after the page loads, and since I wanted a catch all route that redirects to my root_url, that wasn't very appealing.
What I came up with looks like this:
# in routes.rb
get '*ignore_me' => 'site#unknown_url'
# in SiteController
def unknown_url
redirect_to root_url
end
Remember to stick the routes entry at the very bottom of the file!
EDIT:
As Nick pointed out, you can also do the redirect directly in the routes file.
I ran into something like this where I had domain names as a parameter in my route:
match '/:domain_name/', :to => 'sitedetails#index', :domain_name => /.*/, :as =>'sitedetails'
The key piece to this was the /.*/ which was a wildcard for pretty much anything. So maybe you could do something like:
match '/:path/', :to => 'content#holding', :path=> /.*/, :as =>'whatever_you_want'
Where in "routes.rb" is this line located?
To have priority over other routes, it has to be placed first.
As an alternative, you can look into this: http://onehub.com/blog/posts/rails-maintenance-pages-done-right/
Or this: Rails: admin-only maintenance mode

Rails redirecting invalid route to root

If my website is www.foo.com if the user types www.foo.com/blahblahblah it will say that /blahblahblah is an invalid path (obviously). But I want it instead to redirect to the root_path so that the controller can process the URL -- the page www.foo.com should be rendered I want to pull the text blahblahblah and do something with it. How do I do this?
There are several possibilities. Here's one. You could add this to the bottom of your routes.rb:
match ':not_found' => 'my_controller#index',
:constraints => { :not_found => /.*/ }
which will establish a catch-all route to make MyController's index action handle any missing paths; it can detect them by looking at params[:not_found] and doing whatever it wants, such as redirecting to the root_path (redirect_to root_url), redirecting somewhere strategically based on the bad path, rendering something special, examining the referrer/referer for clues about the source, etc.
The :constraints option is necessary; otherwise the not_found param won't be able to contain special characters like slashes and dots.
Put this at the bottom of your routes because, obviously, it will match everything, and you want to give your other routes first crack at the path.
If you only want to redirect, nothing more, you could do this instead (again, at the bottom):
match ':not_found' => redirect('/'), :constraints => { :not_found => /.*/ }
in rails 4
get '*path' => redirect('/')

Resources