2012-07-10 30 views
1

我正在研究這個遺留項目,該項目有一個相當奇怪的設置,我希望擺脫但我的htaccess技能在這個部門有點不足。htaccess規則 - 取代符號鏈接的文件

這是目錄結構。

/index.php 
/www 
    page1.php -> symlink to index.php 
    page2.php -> symlink to index.php 
    page3.php -> symlink to index.php 

/www是公共目錄和人們訪問http://site/page1.php。但是,這些文件中的每一個實際上都與/index.php鏈接。

我覺得這種安排是愚蠢的,希望擺脫符號鏈接,只是簡單地將任何/www/*.php請求指向index.php,而不實際重定向到index.php頁面。

任何可以解決這個問題的htaccess規則的想法?在最基本的核心,我想保持相同的功能,而不必擁有一千個符號鏈接文件。

回答

1

它看起來像index.php文件你的文檔根(這我假設是www),正因爲如此,我不認爲有一種方法,你可以從你的.htaccess文件做到這一點。爲了訪問您的文檔根目錄以外的東西,你需要安裝在任一服務器配置別名或您的虛擬主機配置:

# Somewhere in vhost/server config 
Alias /index.php /var/www/path/to/index.php 

# We need to make sure this path is allowed to be served by apache, otherwise 
# you will always get "403 Forbidden" if you try to access "/index.php" 
<Directory "/var/www/path/to"> 
     Options None 
     Order allow,deny 
     Allow from all 
</Directory> 

現在,你應該能夠訪問/var/www/path/to/index.php。請注意,只要不創建指向它們的Alias(或AliasMatchScriptAlias),/ var/www/path/to目錄中的其他文件就是安全的。現在,你可以通過/index.php URI訪問的index.php,你可以設置在.htaccess文件中的一些mod_rewrite的規則,在您的文檔根目錄(WWW)到點東西的index.php:

# Turn on the rewrite engine 
RewriteEngine On 

# Only apply the rule to URI's that don't map to an existing file or directory 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

# Rewrite all requests ending with ".php" to "/index.php" 
RewriteRule ^(.*)\.php$ /index.php [L] 

這將當你請求http://site/page1.php時,瀏覽器的地址欄不變,但服務器實際上服務於/index.php,它的別名爲/var/www/path/to/index.php

如果需要,可以將正則表達式^(.*)\.php$調整爲更合適的值。這只是匹配任何以.php結尾的內容,包括/blah/bleh/foo/bar/somethingsomething.php。如果要限制目錄深度,可以將正則表達式調整爲^([^/]+)\.php$等。