3

我有一个有两个参数的 url,我已经用这个在 htaccess 文件中重写了它们

RewriteRule ^subscriber/([^/.]+)/([^/.]+)/?$ subscriber/index.php?id=$1&name=$2 [NC,L]

在某些情况下,该名称包含一个“/”,浏览器将其视为另一个参数,并给我一个 404 页面。有没有办法使用 htaccess 文件将“/”替换为其他内容?

4

2 回答 2

1

尝试将其放入文档根目录的 htaccess 文件中:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^(.*)name=([^&]*)/(.*)$
RewriteRule ^ %{REQUEST_URI}?%1name=%2-%3 [L]

# Change your existing rule a bit to account for the slash (plus a possible trailing slash):
RewriteRule ^subscriber/([^/.]+)/(.+?)/?$ /subscriber/index.php?id=$1&name=$2 [NC,L]

这将使 URI 中的第一个“文件夹”在subscriber/之后,id参数,以及除尾斜杠之外的所有内容都成为参数name。然后第一组规则反复清除name参数,替换/-.

于 2012-08-06T05:25:47.630 回答
0

设置名称变量时可以使用 urlencode() 函数

$name = 'Michael/';
$name = urlencode($name);
// will return you Michael%08 or something like this

因此,对于这个问题,.htaccess 的解决方案可能是:

RewriteEngine On
RewriteCond %{REQUEST_URI} (.*)\\(.*)
RewriteRule .* %1/%2 [R=301]
于 2012-08-06T05:07:44.943 回答