2

我的网站有一个子目录:

http://www.mysite.com/test/

在“测试”目录中,我有:

index.php
included-inside-index-file.php
another-included-inside-index-file.php
script.php

现在,我怎样才能用 .htaccess 重写 url,使它像这样工作:

http://www.mysite.com/test --> access index.php
http://www.mysite.com/test/beautiful-url --> access script.php?parameter=beautiful-url
http://www.mysite.com/test/included-inside-index-file.php --> 404 error
http://www.mysite.com/test/another-included-inside-index-file.php --> 404 error
4

1 回答 1

2

这很简单。您需要一个通用规则beautiful-url,但前面是抛出[R=404,L]要隐藏的脚本的规则。

RewriteEngine On

# First hide the includes
# These could be simplified if they follow a pattern.
# Here we'll list them individually though.
RewriteRule ^test/included-inside-index-file\.php - [R=404,L]
RewriteRule ^test/another-included-inside-index-file\.php - [R=404,L]

# Then rewrite to the beautiful URL if the file doesn't exist.
# The index.php will serve normally because it does exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/(.+)$ test/script.php?parameter=$1 [L]

如果你想显式地阻止直接访问script.php,你可以在里面匹配它THE_REQUEST。将其放在阻止访问包含的规则之后。我认为这会奏效。如果您尝试不使用RewriteCond.

RewriteCond %{THE_REQUEST} script\.php
RewriteRule ^. - [R=404,L]
于 2013-01-24T18:01:39.893 回答