Apache重写aaa / bbb / ccc几乎可以工作但不完全

时间:2015-02-11 18:51:57

标签: php apache .htaccess mod-rewrite

我想浏览http://ncvp.co.uk/apache/aaa/bbb/ccc并将aaa / bbb / ccc传递给PHP脚本,同时在浏览器地址栏中保持良好的永久链接。

这是我的.htaccess文件:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) apache/not_index.php?slug=$1

这里是not_index.php:

<b>not_index.php</b> for testing Apache rewrite.<br>
This directory contains <em>.htaccess</em>, <em>not_index.php</em> and the empty directory <em>empty_dir</em>.
<?php
echo "<p>\$_GET:</p>";
echo "<pre>";
print_r($_GET);
echo "</pre>";
?>

一切正常,除非我浏览http://ncvp.co.uk/apache/empty_dir我得到了正确的slug,除了额外的/,但http://ncvp.co.uk/apache/empty_dir/?slug=empty_dir被重写到地址栏。

2月18日

终于修好了!解决方案是添加DirectoryIndex apache/not_index.php,所以.htaccess现在读取:

DirectoryIndex apache/not_index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) apache/not_index.php?slug=$1 [L]

1 个答案:

答案 0 :(得分:0)

这是由于mod_dir在目录后面添加了一个斜杠。你有这样的规则:

# turn auto trailing slash off
DirectorySlash Off
RewriteEngine On
RewriteBase /

# add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ apache/not_index.php?slug=$1 [L,QSA]