Trailing slashes in URLs and how to deal with them

14 views
Skip to first unread message

Shane

unread,
Mar 19, 2021, 11:56:16 AMMar 19
to dotCMS User Group
Hello all,

Something that has been on my mind for some time is how trailing slashes are handled inside dotCMS URLs.

Our current site seems to be ok with both of these URIs and adds a trailing slash if it is missing:
../products/virtual-machines => ../products/virtual-machines/

In dotCMS if you were to hit the path to a page with a trailing slash, you get a 404 page:
../products/virtual-machines/ => 404 error 

Something we have tested is adding in a vanity URL, but to add a vanity URL for every page on our site seems insane.
Are there better ways to handle this?

Thanks!

Mark Pitely

unread,
Mar 19, 2021, 12:18:08 PMMar 19
to dot...@googlegroups.com
You can intercept calls on your 404 page and redirect them.
You can access the request with this:
#set ($thispage=$request.getAttribute("javax.servlet.forward.request_uri"))
#set ($len=$thispage.length())
#set ($prev=$math.sub($len,1))
#if ($thispage.substring($prev,$len)=='/')
#set ($fixed=thispage.substring(1,$prev))
$response.sendRedirect($fixed)
#end

Don't trust that code, it's just pseudocode and totally untested. There might be a smarter way to get the last character.
Hrm, maybe this instead - it's not the 'proper' technique but I think it's a lot easier to parse and understand.
#set ($teststring="${thispage}@")
#if ($teststing.contains("\@"))
#set ($fixed=$teststring.replaceAll("\@',''))


Regardless, put this at the very top of your 404 page code because you want that sendRedirect to happen before any HTML output at all.
If you are going this route, tell us your version and we can tell you how to access the 404 page code.

Lastly, I am sure you can fix this in the OS, if you have access (mod_rewrite in Apache, for example) but that's not really a dotCMS question.

Mark Pitely
Marywood University

--
http://dotcms.com - Open Source Java Content Management
---
You received this message because you are subscribed to the Google Groups "dotCMS User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dotcms+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/dotcms/7eee86c6-f54d-4df3-8d61-ec41c8f720aen%40googlegroups.com.

Shane

unread,
Mar 19, 2021, 12:40:41 PMMar 19
to dotCMS User Group
Mark, that is a supremely interesting way of handling this.

Yeah I had thought about an Apache mod_rewrite too. I wonder if anyone else has tackled this one before and how they might have handled it. 

Shane

unread,
Mar 19, 2021, 3:36:25 PMMar 19
to dotCMS User Group
Oh, I wonder if I can use a RegEx vanity url redirect? Just forward (versus 301/302) folks? 

Mark Pitely

unread,
Mar 20, 2021, 9:52:47 AMMar 20
to dot...@googlegroups.com
Wow, I didn't know that either. Not sure when that was introduced, very nice.
Also, it says this, expressly:

Important Notes

  • Vanity URLs may only contain valid URL characters.
  • All letters in Vanity URLs will be forced lower-case letters.
  • When redirecting TO a file or page, the Vanity URL will work correctly whether or not the Uri field includes a trailing slash (/).
    • BUT to access the Vanity URL from the browser, the Uri will not be recognized if it includes a trailing slash (whether or not the Uri field includes one).
  • You may redirect to a page or file in any folder on your site (even one which is within a folder which is not set to Show on Menu).


My concern is that you'd have to run every single URL through the vanity url process to get this (make a generic path for the whole site) rather than just fix the few organic trailing slashes that occur -might be okay but this is sort of using a flamethrower to light a candle. Unless you know where and why the requests are coming in wrong, in which case you can just use a smaller subset.

M

Reply all
Reply to author
Forward
0 new messages