gpt4 book ai didi

php - .htaccess 重写多个参数的规则

转载 作者:行者123 更新时间:2023-12-01 12:33:11 25 4
gpt4 key购买 nike

重写以下 3 个 URL
http://example.com/index.php?page=searchhttp://example.com/searchhttp://example.com/index.php?page=profile&value=mynamehttp://example.com/mynamehttp://example.com/index.php?page=stats&type=dailyhttp://example.com/stats/daily
当前的 .htaccess 写成如下:

Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&value=([^\s&]+) [NC]
RewriteRule ^ index/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&type=([^\s&]+) [NC]
RewriteRule ^ index/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ index/%1/? [R=302,L,NE]

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

我需要从第一个和第三个 URL 中删除/index/,并从第二个 URL 中删除/index/profile/。

欢迎所有意见和建议。

最佳答案

您不能让搜索和配置文件都使用相同的正则表达式模式。例如,如果有人要求:

http://example.com/index/foo

他们是要访问名为“foo”的页面还是访问名为“foo”的用户的个人资料?您需要添加一些将两者分开的内容,例如:
Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /index\.php\?page=profile&value=([^\s&]+) [NC]
RewriteRule ^ profile/%1? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)&type=([^\s&]+) [NC]
RewriteRule ^ page/%1/%2? [R=302,L,NE]

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+)(\ |$) [NC]
RewriteRule ^ page/%1? [R=302,L,NE]

RewriteRule ^profile/([^/]+)/?$ /index.php?page=profile&value=$1 [NC,L,QSA]
RewriteRule ^page/([^/]+)/([^/]+)/?$ /index.php?page=$1&type=$2 [NC,L,QSA]
RewriteRule ^page/([^/]+)/?$ /index.php?page=$1 [NC,L,QSA]

这使您的网址看起来像:
http://example.com/profile/myname

http://example.com/page/search

http://example.com/page/stats/daily

关于php - .htaccess 重写多个参数的规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31707127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com