问题描述:

I want to browse to http://ncvp.co.uk/apache/aaa/bbb/ccc and pass aaa/bbb/ccc to a PHP script while maintaining the nice permalink in the browser address bar.

This is my .htaccess file:

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule (.*) apache/not_index.php?slug=$1

and here's 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>";

?>

Everything works except if I browse to http://ncvp.co.uk/apache/empty_dir I get the correct slug, apart from an extra /, but http://ncvp.co.uk/apache/empty_dir/?slug=empty_dir is re-written to the address bar.

18 Feb 15

Fixed at last! The solution was adding DirectoryIndex apache/not_index.php, so .htaccess now reads:

DirectoryIndex apache/not_index.php

RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule (.*) apache/not_index.php?slug=$1 [L]

网友答案:

That is due to mod_dir adding a trailing slash after a directory. Have your rules like this:

# 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]
相关阅读:
Top