如何在 .htaccess 中使用正斜杠重定向 url?

How to redirect url with forward slash in .htaccess?

我正在尝试重定向我的 url:

www.site.com/dashboard/invest-package.php?packageID=1

www.site.com/dashboard/invest-package/1

其实我用-

解决了这个问题

<a href="invest-package-'.$row['packageID'].'">Invest</a>

RewriteRule ^invest-package-(.*)$ invest-package.php?packageID= [QSA,L]

但是我想用“/”做,不喜欢用“-”。我在互联网上找到的解决方案不起作用。我不断收到 404 未找到错误。

这是我要投资的link-package.php

<a href="invest-package/'.$row['packageID'].'">Invest</a>

和 .htaccess 文件:

Options -Indexes
RewriteEngine On

RewriteBase /dashboard/

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]

RewriteRule ^invest-package/(.*)$ invest-package.php?packageID= [QSA,L]

您可以试试这个代码:

Options -Indexes -MultiViews
RewriteEngine On
RewriteBase /dashboard/

RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^invest-package/([\w-]+)/?$ invest-package.php?packageID= [QSA,L,NC]

重要的是关闭 MultiViews 即 Apache 的内容协商服务。

选项 MultiViews(参见 http://httpd.apache.org/docs/2.4/content-negotiation.html)由 Apache's content negotiation module 使用,它在 之前 mod_rewrite 运行并使 Apache 服务器匹配文件的扩展名。因此,如果 /file 是 URL,那么 Apache 将在没有任何 GET 参数的情况下提供 /file.php