写.htaccess重写网站URL多个参数

时间:2017-03-18 17:13:19

标签: php .htaccess url-rewriting

我是.htaccess的新手,我在重写某些网址时遇到了问题。

我的网站是用php制作的,有2个参数:param1和param2。 网址类似于:

www.website.com/index.php?param1=12345678
www.website.com/index.php?param1=09876543

www.website.com/index.php?param2=abcdefgh
www.website.com/index.php?param2=qwertzui

我想创建一个.htaccess文件来删除“index.php”,用2个名字替换param1和param2,最后添加“.html”,所以它们变成了:

www.website.com/budget/12345678.html
www.website.com/budget/09876543.html

www.website.com/user/abcdefgh.html
www.website.com/user/qwertzui.html

我有这个代码(从互联网上复制)。 它会删除.php扩展名,但在内部转发时会在URL末尾重写它而忽略参数。

有人帮我编写代码吗?

谢谢:)

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

1 个答案:

答案 0 :(得分:0)

您可以尝试:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# handle ?param1=...
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?param1=([^\s&]+) [NC]
RewriteRule ^ /budget/%1.html? [R=301,L,NE]

# handle ?param2=...
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?param2=([^\s&]+) [NC]
RewriteRule ^ /user/%1.html? [R=301,L,NE]

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,NE,L]

RewriteRule ^budget/([^/.]+)/?$ index.php?param1=$1 [L,QSA,NC]

RewriteRule ^user/([^/.]+)/?$ index.php?param2=$1 [L,QSA,NC]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]