apache - htaccess 重定向 url 带参数

标签 apache .htaccess redirect http-status-code-301

我有数百个这样的链接:

http://www.domain.com/index.php?tag=value

我想将所有链接重定向到
http://www.domain.com/value/

例子:

友情链接1 http://www.domain.com/index.php?tag=LW1fdX49tR重定向到:http://www.domain.com/LW1fdX49tR/
友情链接2 http://www.domain.com/index.php?tag=A3kh0QLIrc重定向到:http://www.domain.com/A3kh0QLIrc/
友情链接3 http://www.domain.com/index.php?tag=vXwNR4U9qY重定向到:http://www.domain.com/vXwNR4U9qY/
等等

我怎样才能做到这一点?
谢谢!

最佳答案

除了重定向请求之外,您可能还想确保新 url 也确实有效。为此,您将需要外部重定向和内部重写。在下面的示例中,我使用 THE_REQUEST 技巧仅在它是实际请求 url 时触发规则,如果它在内部重写,则不会触发。需要防止无限循环。

#External redirect with THE_REQUEST trick; change R to R=301 when everything works correctly
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /index\.php\?tag=(.*)\ HTTP
RewriteRule ^ /%2? [R,L]

#Internal rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?tag=$1 [L]

关于apache - htaccess 重定向 url 带参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22567357/

相关文章:

macos - 在 Mac OS X Mountain Lion 上编译 apache2 的问题

apache - 使用参数重定向 URL

javascript - 内部问号或 angularjs 的 url 出现问题

Apache .htaccess 密码通过相对路径保护

java - org.apache.jasper.JasperException :/index. jsp

css - Fontawesome 加载不出来

php - 防止php使用默认用户名和密码到mysql

python - 当我执行 HttpResponseRedirect (302) 时,Django 登录 POST 挂起

javascript - jQuery Click anchor 标记防止页面滚动到顶部

Javascript location.href 在 Explorer 11 上崩溃