【问题标题】:Problem with .htaccess (mod_rewrite). RewriteRule's doens't work correctly.htaccess (mod_rewrite) 的问题。重写规则无法正常工作
【发布时间】:2025-12-10 00:20:05
【问题描述】:

我的两个 RewriteRules 有问题。

.htaccess:

# protect the htaccess file
<files .htaccess>
 order allow,deny
 deny from all
</files>

RewriteEngine On
Options +FollowSymlinks
Options -Indexes
RewriteBase /test/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^download/([0-9]+)$ download.php?id=$1 [L]
RewriteRule ^(.*)$ index.php?c=view&misc=$1 [B]

如果 url 包含下载(有些像这样:mydomain.com/download/9),第一条规则应该将此请求改写为 download.php?id=9。但事实并非如此。

var_dump($_GET) 显示如下:

array(2) { ["c"]=>  string(4) "view" ["misc"]=>  string(9) "index.php" } index.php

有什么想法吗?

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    好的,我解决了这个问题。

    # protect the htaccess file
    <files .htaccess>
     order allow,deny
     deny from all
    </files>
    
    RewriteEngine On
    Options +FollowSymlinks
    Options -Indexes
    
    RewriteBase /test/download/
    RewriteRule ^download/([0-9]+)$ download.php?id=$1 [L]
    
    RewriteBase /test/
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ index.php?c=view&misc=$1 [B]
    

    谢谢各位! :)

    【讨论】:

      【解决方案2】:

      问题不在于规则。它在这里工作。

      http://wingsoflol.org/download/9

      我的 download.php 包含

      Hi!
      <br>Id = <?
      echo $_GET['id'];
      ?>
      

      规则是

      RewriteRule ^download/([0-9]+)$ download.php?id=$1 [L]

      你确定 RewriteBase 应该是 /test/ 吗?

      【讨论】: