繁体   English   中英

mod_rewrite在传递给php之前检查公用文件夹

[英]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

因此,例如以下是一些请求和响应:

  • example.com/test.xml >>> /public/test.xml
  • example.com/a_folder/ >>> /public/a_folder/index.html
  • example.com/a_folder/test >>> /public/a_folder/test.html
  • example.com/not_in_public >>> /index.php

在此先感谢任何对此的指点。

像这样的东西:(从我的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM