【发布时间】:2016-08-21 12:04:45
【问题描述】:
您好,我想通过 htaccess 自定义重定向。
仅适用于这种格式
www.example.com/index.php?id=abc --> www.example.com/abc
但对于任何其他格式没有变化:
例如
www.example.com/index.php?id=abc&id2=qaz --> www.example.com/index.php?id=abc&id2=qaz
这段代码(@starkeen 编写)做得很好:
RewriteEngine on
#1)Redirect "/index.php?id=foo" to "/foo"#
RewriteCond %{THE_REQUEST} /(?:index\.php)?\?id=([^\s&]+)\sHTTP [NC]
RewriteRule ^ /%1? [L,R]
#2)The rule bellow will internally map "/foo" to "/index.php?id=foo"#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /index.php?id=$1 [L]
现在如何添加自定义参数名称,如 id2(或更多)?
例如
www.example.com/index.php?id=abc --> www.example.com/abc
www.example.com/index.php?id=abc&id2=qaz --> www.example.com/abc/qaz
但是(这里)对此没有改变:
www.example.com/index.php?id=abc&id2=qaz&id3=wsx --> www.example.com/index.php?id=abc&id2=qaz&id3=wsx
【问题讨论】:
标签: .htaccess url redirect mod-rewrite url-rewriting