2012-11-13 140 views
0

我希望你能幫上忙。我試圖寫一個htaccess文件做下面的事情。htaccess rewritecond不工作

1)重定向到www。從URL

3)地址

2)刪除.PHP如果文件不存在,則使用filechecker.php?頁=文件名


1,我可以做

的RewriteCond%{HTTP_HOST}^example.com $

重寫規則(。*)http://www.example.com/ $ 1 [R = 301,L]


2)I可與

的RewriteCond%{SCRIPT_FILENAME}做.PHP -f

重寫規則[^ /] $%{REQUEST_URI} .PHP [QSA,L]


3)我想

的RewriteCond%{} REQUEST_FILENAME .PHP!-f

重寫規則^([^/] *)$ filechecker.php?page = $ 1 [QSA,L]

會工作,但由於某些原因,它忽略了頁面確實存在的事實。


我希望你能幫助 馬克

回答

0

您的2將循環解決方案,但足夠簡單修復,沿着你的文件中以下行堅持東西部2與3:

#If the Browser request contains a .php, instruct the browser to remove it. 
RewriteCond %{THE_REQUEST}  \.php  [NC] 
RewriteRule ^/?(.*)\.php$  http://%{HTTP_HOST}/$1   [R=301,NC,L] 

#If a request is received for a non file-system object, that doesn't have a .php suffix, then store the full path, filename, and URI in a variables with that extention. 
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-s 
RewriteCond %{SCRIPT_FILENAME} !-f 
RewriteCond %{SCRIPT_FILENAME} !^\.php$ [NC] 
RewriteRule ([^/]+)$  - [E=testScript:%{SCRIPT_FILENAME}.php,E=testFile:$1.php,E=testURI:%{REQUEST_URI}.php] 

#See if the file exists with a .php extention, if it does internally rewrite 
RewriteCond %{ENV:testScript} -s [OR] 
RewriteCond %{ENV:testScript} -f 
RewriteRule .*    %{ENV:testURI} [L] 

#Else if a ENV:testDile is set, then pass the name to the php script 
RewriteCond %{ENV:testFile} !^$ 
RewriteRule .*    /filechecker.php?page=%{ENV:testFile} [L] 

供參考:Apache mod_rewrite文檔值得一讀,如果您不清楚上述規則是做什麼的,或者您想要更抽象一些的東西,請考慮以下post