.htaccess外部&内部重写冲突

时间:2018-04-03 12:22:12

标签: apache .htaccess

我正在尝试将查询字符串重写为路径,如下所示:

const data = {"username":"hamet","username_Type":"string","meta":"object","meta_Type":"object","meta.avatar":"/avatar.png","meta.avatar_Type":"string","meta.active":"false","meta.active_Type":"boolean","meta.someArr":"array","meta.someArr_Type":"array","meta.someArr.0":"object","meta.someArr.0_Type":"object","meta.someArr.0.field":"123","meta.someArr.0.field_Type":"number","meta.someArr.1":"object","meta.someArr.1_Type":"object","meta.someArr.1.field":"321","meta.someArr.1.field_Type":"number"}

const result = {}
const create = {'string': String,'number': Number,'boolean': Boolean,'array': Array,'object': Object}
const findType = (key, obj) => obj[key]

Object.keys(data).forEach(key => {
  if (!key.includes('Type')) {
    key.split('.').reduce((r, e, i, arr) => {
      let type = findType(key + '_Type', data);
      let value = create[data[key]] || arr[i + 1] ? new create[type] : new create[type](data[key]).valueOf()
      if (data[key] == 'false') value = false
      r[e] = r[e] || value;
      return r[e]
    }, result)
  }
})

console.log(result)

http://example.com/?p=page1

内部重定向有效,我可以在第二个网址查看该页面,但只要我尝试将第一个网址重定向到第二个网址,我就会收到“重定向太多”错误。

htaccess的:

http://example.com/page/page1

前两行是自己工作的。添加最后两行会导致错误。

1 个答案:

答案 0 :(得分:1)

你的规则是将uri重定向回来,这就是你遇到重定向错误的原因。 您可以使用%{THE_REQUEST}%{ENV_REDIRECT_STATUS}变量来避免Too many redirect错误。

RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{ENV_REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE,QSD,NC]

RewriteCond %{REQUEST_URI} /page/(.*)
RewriteRule page/(.*)$ index.php?p=$1 [L,QSA,NC]
RewriteCond %{THE_REQUEST} /\?p=.+ [NC]
RewriteCond %{QUERY_STRING} ^p=(.*)$ [NC]
RewriteRule (.*) /page/%1? [R=301,L,NE]