2013-01-24 149 views
2

我有我的網站的子目錄:URL重寫和文件夾

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

裏面的「測試」的目錄,我有:

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

現在,我怎麼可以重寫URL以這樣的.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 

回答

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] 
+0

謝謝!這應該工作!只要我測試它,我會標記你的答案是正確的。 – Stefano

+0

@Stefano爲你做了這個工作嗎? –