How to redirect to mobile page based on m.mysite.c

2019-03-05 17:41发布

I have a grails app I am working on at my current job. I have to work on the mobile version of the website and I want to catch in UrlMappings.groovy whether the request is coming from www.mysite.com or m.mysite.com. Any idea how can i do that?

UPDATE:

My current mappings look like this --->

import static org.apache.commons.lang.StringUtils.*

class UrlMappings {
    static mappings = {
        "/$controller/$action?/$id?"{
            constraints {
                // apply constraints here
            }
        }

        "/$lang/$controller/$action?/$id?"{
            language = "${lang}"
            constraints {
                // apply constraints here
            }
        }

        // Start :: Shopping Tools internal URLs
        "/modelLine/build/$year/$modelLineCode/"(controller:'modelLine', action:'build')
        "/colors/index"(controller:'colors', action: 'index')
        "/packages/index"(controller:'packages', action: 'index')
        "/accessories/index"(controller:'accessories', action: 'index')
        "/summary/index"(controller:'summary', action: 'index')

        // Start :: Spanish :: Shopping Tools internal URLs

        "/$lang/modelLine/build/$year/$modelLineCode/"(controller:'modelLine', action:'build')
        "/$lang/colors/index"(controller:'colors', action: 'index')
        "/$lang/packages/index"(controller:'packages', action: 'index')
        "/$lang/accessories/index"(controller:'accessories', action: 'index')
        "/$lang/summary/index"(controller:'summary', action: 'index')

        // End :: Spanish :: Shopping Tools internal URLs
        // End :: Shopping Tools internal URLs


        // Default Home Page
        "/$lang?"(controller:"modelLine", action:"index"){
            constraints {
                lang inList:['en','fr'] // avoids invalid matching
            }
        }

        "/admin"(controller:"admin", action:"index")
        // Error page definitions
        "404"(controller:"errors", action:"notFound")
        "500"(controller:"errors", action:"serverError")
        "403"(view:'/login/denied')
        "402"(view:'/login/denied')
        "401"(view:'/login/denied')
    }
}

How do I change them to detect an m.mysite.com request?

4条回答
我命由我不由天
2楼-- · 2019-03-05 18:25

You can create a filter or Interceptor in Grails 3 and check request.getHeader("User-Agent") if it is mobile then redirect the url where ever you want.

查看更多
爷、活的狠高调
3楼-- · 2019-03-05 18:31

It's in the request object, available in all controller actions as request.

Check the available properties here https://grails.github.io/grails-doc/3.0.x/ref/Servlet%20API/request.html

查看更多
女痞
4楼-- · 2019-03-05 18:32

I solved this by changing the url at the apache level. Changed it to www.mysite.com/m

查看更多
ゆ 、 Hurt°
5楼-- · 2019-03-05 18:35

If you have different controllers for mobile site, why don't you change all links in your m.mysite.com for specified mobile mapping? For exampleController and action somethingMobile in mobile:

m.mysite.com/mobile/example/somethingMobile

the same for desktop but with action something:

mysite.com/example/something
查看更多
登录 后发表回答