简体   繁体   English

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

[英]write .htaccess rewrite website URL multiple parameters

I´m new to .htaccess and I am having problems in rewriting some URLs. 我是.htaccess的新手,我在重写某些网址时遇到了问题。

My website is made with php and has 2 parameters: param1 and param2. 我的网站是用php制作的,有2个参数:param1和param2。 The URLs look like similar to: 网址类似于:

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

I would like to create a .htaccess file to remove “index.php”, replace param1 and param2 with 2 names and add “.html” at the end, so they became: 我想创建一个.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

I have got this code (copied from internet). 我有这个代码(从互联网上复制)。 It removes the .php extension but in the internally forward it rewrite it at the end of the URL neglecting the parameters. 它会删除.php扩展名,但在内部转发时会在URL末尾重写它,忽略参数。

Is someone so kind to help me to write the code? 有人帮我写代码吗?

Thanks :) 谢谢 :)

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]

You can try: 你可以试试:

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]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM