使用htaccess从url中删除参数名称

时间:2014-07-22 08:58:00

标签: regex apache .htaccess url mod-rewrite

我有以下网址

http://localhost/mybb/index.php?url=widgetboard/questions&step=1&id=1

在应用重写规则

后变为以下URL
http://localhost/mybb/widgetboard/questions/step/1/id/1

是否可以删除2个单词,即" step"和" id"从网址开始,在网址的最后添加.html

我的代码是

Options -MultiViews

# turn rewriting on
RewriteEngine On

# When using the script within a sub-folder, put this path here, like /mysubfolder/
# If your app is in the root of your web folder, then please delete this line or comment it out
RewriteBase /mybb/

RewriteRule ^(.+?)/(step)/([0-9]+)/(id)/([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]

RewriteRule ^(.+?)/(category)/([0-9]+)/(answer)/([0-9]+)/?$ index.php?url=$1&$2=$3&$4=$5 [NC,L,QSA]

RewriteRule ^(.+?)/(step)/([0-9]+)/?$ index.php?url=$1&$2=$3 [NC,L,QSA]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?url=$1&$2=$3 [NC,L,QSA]

2 个答案:

答案 0 :(得分:1)

您可以RewriteBase /mybb/下面插入此规则:

RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/([0-9]+)/?$ index.php?url=$1/$2&step=$3&id=$4 [L,QSA]

答案 1 :(得分:0)

将您的第二条规则更改为:

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

然后,如果用户点击此网址:domain.com/3/120,您的PHP脚本将会收到index.php?step=3&id=120