[英]php-fpm - How to execute certain symlinks as PHP scripts
我用FastCGI和php-fpm運行Apache 2.2。 我試圖復制以下邏輯:
<FilesMatch "^(admin|api|app)?(_dev)?$">
#ForceType application/x-httpd-php
SetHandler php-fcgi
</FilesMatch>
這允許我將admin.php作為管理員進行符號鏈接,因此我可以刪除.php擴展名。 似乎用php-fpm執行此操作的唯一方法是將www.conf
文件的security.limit_extension
設置為空,但是,正如評論所示,這是一個相當大的安全漏洞,因為php代碼現在可以從任何文件中執行,無論擴展名如何。
什么是實現上述目標的首選方法,但仍然保持一定的安全性?
到目前為止,看起來最好的解決方案是手動將已知的符號鏈接添加到列表中(位於/etc/php-fpm.d/www.conf中):
security.limit_extension php admin admin_dev api api_dev app app_dev
不確定security.limit_extension指令是否甚至可以采用正則表達式,看起來不像它,所以這就像它得到的一樣好。 如OP中所述,您仍然需要在vhost配置中維護filesmatch指令:
<FilesMatch "^(admin|api|app)?(_dev)?$">
SetHandler php-fcgi
</FilesMatch>
- 更新 -
根據tftd的注釋,添加當前重寫指令:
RewriteBase /
# we skip all files with .something
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
@Mike,根據你更新的答案,類似於這個.htaccess
文件應該能夠處理你想要做的事情:
# Enable the rewrite engine
RewriteEngine on
# Set the rewrite base path (i.e. if this .htaccess will be running at root context "/" or a subdir "/path")
RewriteBase /
# If the file exists, process as usual.
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [NC,L]
# If the dir exists, process as usual (if you don't need this, just comment/remove the next two lines).
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [NC,L]
# If (requested_file_name).html exists, rewrite to that file instead.
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [QSA,L]
# If (requested file name).php exists, rewrite to that file instead.
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.html [QSA,L]
# If none of the above rules were triggered, fallback to index.php.
RewriteRule ^(.*)$ index.php [QSA,L]
通過一些調整,這應該能夠完成工作,而無需深入了解httpd.conf
和<VirtualHost>
以及<FilesMatch>
指令。 希望這可以幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.