【问题标题】:php .htaccess shows 404 instead of redirectingphp .htaccess 显示 404 而不是重定向
【发布时间】:2016-03-20 00:43:07
【问题描述】:

我正在尝试让我的 .htaccess 文件将所有内容重定向到 index.php 并使用 url 参数来显示我需要的内容。

如果我输入类似“http://localhost/2351/515”的内容,那么它只会显示 404 not found 而不是使用参数重定向。

index.php

<?php
    if (isset($_GET['params'])) {
        $params = explode( "/", $_GET['params'] );
        print_r($params);
        exit("YUP!");
    }
?>

.htaccess

ServerSignature Off

Options -Indexes
Options +FollowSymLinks

RewriteEngine On

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

RewriteRule ^(.*)$ index.php?p=$1 [NC]

【问题讨论】:

  • 首先,您将(.*) 传递给p 参数,而不是param 参数。所以你正在阅读不正确的东西。但是,这不会导致 404,这可能是因为 mod_rewrite 可能已禁用,或者您的虚拟主机配置中未设置 AllowOverride All

标签: php apache .htaccess mod-rewrite


【解决方案1】:

试试看:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

【讨论】:

  • OP 想要读取param 查询参数。这样做不会创建该参数。
猜你喜欢
  • 2013-03-26
  • 2015-08-03
  • 2013-12-01
  • 2018-10-17
  • 2021-05-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多