Hello everyone! I have a handler that handles rout...
# cfml-beginners
o
Hello everyone! I have a handler that handles routing to a few legal pages. Here is a snip:
Copy code
component extends="coldbox.system.EventHandler" {

	// default Action
	function index( event, rc, prc ){
		event.setHTTPHeader( statusCode = 400 );
		relocate(uri = "/");
	}

	// terms of service Action
	function termsofservice( event, rc, prc ){
		prc.pageTitle = "Terms of Service";
		event.setView( "legal/termsofservice" );
	}
...
}
I added a default action to redirect the user to the home page if they try to access the root of the uri example:
localhost:8080/legal
and set the status code to
400 Bad Request
. Is this correct or should i be using a different status code?
r
I would think the relocate would probably change the status code to 301 when it redirected
But that should work
o
@Robert Zehnder Am I using the correct status code (400) or should I change it to 301?
r
You should be able to do this
Copy code
// default Action
	function index( event, rc, prc ){
		relocate(uri = "/", statusCode = 400);
	}
I generally handle this in my routes.cfc instead of routing by convention
o
@Robert Zehnder How would you handle the situation where the handler shouldn't have a default action? Do you redirect them or show a page listing the handlers pages.
r
Sorry, it has been a busy day.
I would handle it just the way you did.
o
My fault, didn't mean to bother you. Didn't see that you were the lead of a company.
r
relocate()
in the handler. I would not bother explicitly setting the response code, just do the redirect
No bother at all
o
Where do you redirect to in this type of situation, to the home page?
r
That is up to you, but the home page is a likely choice
o
Thank You very much for helping me
💯 1
r
My pleasure
b
You can try to redirect the user somewhere, but honetly if a user can created their own URL to your server that is bogus, unless you can make a guess of where they wanted to go, you may as well just allow the default ColdBox "event not found" handler to kick in which you can customize to say whatever you want.
400 is not a valid response header as it implies there is an actual issue with the semantics of the HTTP request itself. You would want 404 to indicate the resource was not found
👍 1
But that said, you can't set a 40x status code AND redirect (which uses a 30x status code). You need to pick one. I would personally recommend you setting the 404 in your coldbox "not found" handler, which you only need to setup once and then it can apply to an infinite number of invalid events
Using any sort of redirect may provide a slightly nicer user experience, but it will also trick bots like Google into thinking invalid URls are in fact valid.
Better IMO to simply tell the user "hey we can't find that page" and return the 404 so search engines know as well. This means simply deleting the entire
index()
function from your code, running the termsofservice() event inside of it if you want
/legal
to show the same things as
legal/termsOfService
!
We don't have any special boilerplate handlers in place to try and guess all the possible invalid events a user may hit. We just have the site wide handler which handles it nicely with a 404 status code and prompts them with a link back home.
@Ookma-Kyi
I would recommend further ColdBox questions being asked in the #box-products channel just since this channel is generally used for more general questions about CFML itself. You're more likely to hit a box expert in the dedicated channel 🙂