Forum Discussion

shafiul's avatar
shafiul
Icon for Nimbostratus rankNimbostratus
Aug 08, 2016

Set redirect to non trailing slash

We need to setup 301 redirect from all urls, starting from https://test.com/SOMECONTENT/ to appropriate URL on https - https://test.com/SOMECONTENT but without trailing slash. The same is for http - http://test.com/SOMECONTENT/ to https - https://test.com/SOMECONTENT

 

2 Replies

  • Hi Shafiul,

    you may try the iRule below...

    when HTTP_REQUEST {
        if { ( [string tolower [HTTP::path]] starts_with "/somecontent/" ) and ( [HTTP::path] ends_with "/" ) } then {
            if { [HTTP::query] eq "" } then {
                HTTP::respond 301 "Location" "https://[getfield [HTTP::host] ":" 1][string range [HTTP::path] 0 end-1]"
            } else {
                HTTP::respond 301 "Location" "https://[getfield [HTTP::host] ":" 1][string range [HTTP::path] 0 end-1]?[HTTP::query]"
            }
        } else {
             Request to other base directory or a path without trailing / detected
        }
    }
    

    Note: The iRule can be used for HTTP and HTTPS access as well.

    Cheers, Kai

  • Hi Shafiul,

    I've included

    /somecontent/
    in my example, because you've asked for this... 😉

    Your modified version should include at least an additional exemption for the

    /
    (www-root) of your site. Otherwise a redirect loop may occour...

    when HTTP_REQUEST {
        if { ( [HTTP::path] ends_with "/" ) and not ( [HTTP::path] equals "/" ) } then {
            if { [HTTP::query] eq "" } then {
                HTTP::respond 301 "Location" "https://[getfield [HTTP::host] ":" 1][string range [HTTP::path] 0 end-1]"
            } else {
                HTTP::respond 301 "Location" "https://[getfield [HTTP::host] ":" 1][string range [HTTP::path] 0 end-1]?[HTTP::query]"
            }
        } else {
             Request to other base directory or a path without trailing / detected
        }
    } 
    

    Cheers, Kai