[英]mod_rewrite check public folder before passing to php
我要達到的目的(使用mod_rewrite)是在請求時檢查公用文件夾中是否存在文件/目錄,如果存在,則將其提供,否則,將請求路由到index.php文件。 另外,如果URL中未提供擴展名,則默認為.html。
下面是我的文件夾結構:
/.htaccess
/index.php
/public
/test.html
/test.xml
/a_folder
/index.html
/test.html
因此,例如以下是一些請求和響應:
在此先感謝任何對此的指點。
像這樣的東西:(從我的WordPress .htaccess文件中大量復制)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)^(\.\w+) $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
$1.html
行不在我的腦海中,可能需要一些調整。 請注意,這是一項愚蠢的檢查,即所請求的URL以點號結尾,后跟一個或多個單詞字符。
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(|index\.php)$ - [L]
RewriteRule ^public/(.*)$ - [L]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [R]
</IfModule>
這應該可以解決問題。
基本上,如果它是/
或/index.php
它什么都不做,如果它是/public/whateveryouwant
它什么也不會做,如果它是其他任何東西,它將重寫為/public/whateveryouwant
並檢查它是文件還是文件。目錄。 如果不是,它將重定向到index.php
。
感謝@ jxpx777和@ N1xx1,經過一點點的雜耍使其得以工作。
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^public/(.*)$ - [L]
# Check public cache
RewriteRule ^(.*)$ public/$1
RewriteRule (.*)/$ $1/index
# Add .html if not extension provided
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^(.*)$ $1.html
# Push through stack in not in cache
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.