简体   繁体   English

如何获得请求引荐来源域名? 没有子域[RAILS]

[英]How to get request referrer domain? Without subdomain [RAILS]

For example: 例如:

@ref = 'http://sub.domain.com'
URI.parse(@ref).host 

returns 'sub.domain.com'. 返回“ sub.domain.com”。 How to get 'domain.com' only? 如何仅获取“ domain.com”?

Use request.domain(1) to get 'domain.com'. 使用request.domain(1)获取“ domain.com”。

Per the Rails Guides section about the request object , the request.domain property takes an optional argument, ie request.domain(n) returns 根据关于请求对象的Rails Guides部分, request.domain属性采用一个可选参数,即request.domain(n)返回

The hostname's first n segments, starting from the right (the TLD). 主机名的前n个分段,从右侧(TLD)开始。

It's not obvious from this description, but it's actually starting from the dot before the TLD; 从这个描述来看不是很明显,但实际上是从TLD之前的点开始的。 so given a request object for 'sub.domain.com', request.domain(0) would return '.com' and request.domain(1) would return 'domain.com'. 因此,给定“ sub.domain.com”的请求对象, request.domain(0)将返回“ .com”,而request.domain(1)将返回“ domain.com”。

将正则表达式应用于URI.parse(@ref).host仅返回域

URI.parse(@ref).host.match(/\w*.com$/)[0]

只需使用请求。

request.domain

To get the domain name you have to use the below 要获取域名,您必须使用以下内容

request.domain

If you use 如果您使用

@ref = 'http://sub.domain.com'
URI.parse(@ref).host ====> It will give you host name(Both domain and subdomain)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM