如何使用.htaccess为多个参数创建干净的URL [英] How to create clean url using .htaccess for multiple parameters
问题描述
这是我的.htaccess代码,用于重写干净的网址.
This is my .htaccess code to rewrite clean url.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+download\.php\?id=([^\s&]+) [NC]
RewriteRule ^ download/%1? [R=301,L]
RewriteRule ^download/([^/]+)/?$ download.php?id=$1 [L,QSA]
此代码对于url中的单个参数正常工作.例如,它将www.mysitename.com/download.php?id=123
重写为www.mysitename.com/download/123
This code works fine for single parameter in url. For example it rewrites www.mysitename.com/download.php?id=123
to www.mysitename.com/download/123
但是当我尝试在url中传递多个参数时,我得到的只是错误.搜索了各种资源和相关问题,但没有适当的解决方案.
But when I tried to pass multiple parameters in url, all I got are errors. Searched various resources and related questions but didn't got proper solution.
我需要一个类似www.mysitename.com/download/123/file-name
的网址,而不是www.mysitename.com/download.php?id=123&name=file-name
I need a url like www.mysitename.com/download/123/file-name
instead of www.mysitename.com/download.php?id=123&name=file-name
我想我必须使用类似RewriteRule ^(.*)/(.*)$ download.php?id=$1&name=$2
这样的东西.但是在实施时出现了404错误.如何更改我的代码以传递多个URL.预先感谢.
I guess I've to use something thing like this RewriteRule ^(.*)/(.*)$ download.php?id=$1&name=$2
. But while implementing I'm getting 404 error. How can I alter My code to pass multiple urls. Thanks in advance.
推荐答案
您只需要在规则中添加参数
You just need to add a parameter in your rule
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+download\.php\?id=([0-9]+)&name=([^\s&]+)\s [NC]
RewriteRule ^ download/%1/%2? [R=301,L]
RewriteRule ^download/([0-9]+)/([^/]+)/?$ download.php?id=$1&name=$2 [L]
这篇关于如何使用.htaccess为多个参数创建干净的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!