【问题标题】:Mod rewrite htaccess - two variables in rewrite ruleMod rewrite htaccess - 重写规则中的两个变量
【发布时间】:2014-08-26 10:35:55
【问题描述】:

我在网上找到了这个解决方案:

RewriteEngine On
RewriteBase /test/

RewriteRule ^([^-]*)/$ index.php?page=$1
RewriteRule ^([^-]*)/([^-]*)/$ index.php?page=$1&link=$2 [L]

#dodaje slash na koncu
RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L]

第一个 RewriteRule 完美运行,它返回给我$_GET['page']=130。但是当涉及到第二个时,它返回给我$_GET['page']=index.php 而不是$_GET['page']=130$_GET['link']=35。由于页面的数字 id,以 SQL 错误结束。

普通链接如下:

?page=136

?page=136&link=35

改写一个:

/136/ - 有效

/136/35/ - 不起作用,$_GET['page']=index.php

【问题讨论】:

  • 究竟您要访问的网址是什么?
  • link link
  • 您想联系/130/35/130/35/ 吗?因为你的规则只有在有斜杠时才匹配
  • 不,RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-]+)$ $1/ [NC,R=301,L] 在末尾添加斜线。

标签: apache .htaccess mod-rewrite url-rewriting rewrite


【解决方案1】:

你可以用这个替换你当前的代码(你的htaccess必须在test文件夹中,index.php也是一样)

RewriteEngine On
RewriteBase /test/

# add trailing slash if no trailing slash and not an existing file/folder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+[^/])$ $1/ [R=301,L]

RewriteRule ^([^/]*)/([^/]*)/$ index.php?page=$1&link=$2 [L]
RewriteRule ^([^/]*)/$ index.php?page=$1 [L]

你可以试试这些链接

  • http://domain.com/test/136/35/ 内部重写index.php?page=136&link=35
  • http://domain.com/test/136/35 重定向http://domain.com/test/136/35/
  • http://domain.com/test/136/ 内部重写index.php?page=136
  • http://domain.com/test/136 重定向http://domain.com/test/136/

【讨论】:

  • 谢谢伙计。这完美无缺。都是关于重写的顺序吗?
  • 您的代码中有很多错误。我已经重写了您的尾部斜杠规则并更改了您的两条规则中的模式(此代码的顺序并不重要)
猜你喜欢
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 2013-01-13
  • 2015-03-23
  • 2013-07-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多