Mod重写htaccess - 重写规则中的两个变量

时间:2014-08-26 10:35:56

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

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

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

1 个答案:

答案 0 :(得分:2)

你可以用这个替换你当前的代码(你的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/