【问题标题】:How to redirect url with forward slash in .htaccess?如何在.htaccess中使用正斜杠重定向url?
【发布时间】:2021-10-02 07:43:45
【问题描述】:

我正在尝试重定向我的网址:

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=$1 [QSA,L]

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

这是我到invest-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=$1 [QSA,L]

【问题讨论】:

    标签: php apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    你可以试试这个代码:

    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=$1 [QSA,L,NC]
    

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-14
      • 1970-01-01
      • 1970-01-01
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 2013-04-09
      相关资源
      最近更新 更多