.htaccess URL 重写中的 3 个斜杠


3 Slashes in .htaccess URL rewrite

基本上我创建了一个PHP网站,它使用其他网站的API来提取数据。我的问题是我收到错误 404 .

我在下面添加了代码以.htaccess

 RewriteRule ^sort/(.*) index.php?by=$1

这 website.com/sort/gender 工作了!

但是我有另一个页面名称用户.php我已添加

RewriteRule ^profile/(.*) user.php?name=$1

制作website.com/profile/username

但我想展示

website.com/profile/username/bookmarks

而不是

website.com/profile/username?by=bookmarks

我的完整.htaccess代码如下:

RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
ErrorDocument 404 /404.php
RewriteRule ^sort/(.*) index.php?by=$1
RewriteRule ^profile/(.*) user.php?name=$1
RewriteRule    ^privacy/?$    privacy.php    [NC,L]

您现有的重写规则中缺少一些标志和不正确的正则表达式。

你完整的.htaccess将是这样的:

ErrorDocument 404 /404.php
RewriteEngine on
Options +FollowSymLinks -MultiViews
RewriteBase /
RewriteRule ^sort/([^/]+)/?$ index.php?by=$1 [L,NC,QSA]
RewriteRule ^(profile)/([^/]+)/([^/]+)/?$ $1/$2?by=$3 [L,NC,QSA]
RewriteRule ^profile/([^/]+)/?$ user.php?name=$1 [L,NC,QSA]
RewriteRule ^privacy/?$ privacy.php [NC,L,QSA]

您可以添加另一个包含 2 个通配符的重写规则,如下所示:

RewriteRule ^profile/(.*)/(.*) user.php?name=$1&by=$2
RewriteRule ^profile/(.*) user.php?name=$1