我的网站有一个子目录:
http://www.mysite.com/test/
在“test”目录中我有:
index.php
included-inside-index-file.php
another-included-inside-index-file.php
script.php
现在,我如何用.htaccess重写网址,所以它的工作原理如下:
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
答案 0 :(得分: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
的情况下进行此操作,您将从美丽的网址重写404.
RewriteCond %{THE_REQUEST} script\.php
RewriteRule ^. - [R=404,L]