2012-02-07 30 views
0

斜線我有很多文件的開頭「顯示 - 」,如:%REQUEST_URI沒有在.htaccess

display-profile.php 
display-photos.php 
display-comments.php 
... 

我想保持這些名稱的組織(因爲有與其他前綴的文件),但我想從我的網址中移除「display-」。
我打算使用.htaccess來檢查display-%{REQUEST_URI}是否是一個文件,但該環境變量在開始處有一個斜槓「/」。

我如何獲得%{REQUEST_URI}一開始沒有斜線?或者,還有其他方法可以檢查嗎?

謝謝。我在Linux上使用Apache。

回答

2
RewriteEngine on 
RewriteBase/
RewriteCond %{DOCUMENT_ROOT}/display-$1 -f 
RewriteCond %{REQUEST_URI} !display-([^/]+).php$ 
RewriteRule ([^/]+)$ display-$1 [L,QSA] 

或者,另一種選擇:

RewriteEngine on 
RewriteCond %{REQUEST_URI} .(.+)$ 
RewriteCond %{DOCUMENT_ROOT}/display-%1 -f 
RewriteRule . display-%1 [L,QSA] 

可能更短:

RewriteEngine on 
RewriteBase/
RewriteCond %{DOCUMENT_ROOT}/display-$1 -f 
RewriteRule ^(.+)$ display-$1 [L,QSA] 

甚至:

RewriteEngine on 
RewriteCond %{DOCUMENT_ROOT}/display-$1 -f 
RewriteRule ^/?(.+)$ display-$1 [L,QSA]