将所有文件视为参数,几乎没有例外

时间:2016-01-06 09:56:28

标签: regex apache .htaccess mod-rewrite

更新:更改了此问题的标题以反映我想要的内容

我希望将所有目录中的所有文件都视为参数,除了/ public /目录及其所有子目录中的所有文件,使用.htaccess。

我尝试过很多东西,但都没有用。我当前的.htaccess文件如下所示:

DirectorySlash Off
RewriteEngine On
RewriteBase /

# remove access to .htaccess
RewriteCond %{THE_REQUEST} /\.htaccess [NC]
RewriteRule ^ - [F]

# remove access to launcher.php
RewriteCond %{THE_REQUEST} /launcher\.php [NC]
RewriteRule ^ - [F]

# enforce HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# treat existing directories as parameters
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]    

# handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ launcher.php?url=$1 [QSA,L]

此文件已执行以下操作:

  • 将所有目录(无论是否存在)视为参数
  • 不允许访问.htaccess和launcher.php
  • 强制执行HTTPS
  • 将所有请求重定向到launcher.php

请注意我希望将所有文件作为参数处理,而不仅仅是不允许访问。例外情况是/ public /文件夹及其所有子目录中的所有文件。

1 个答案:

答案 0 :(得分:1)

您可以使用此.htaccess:

DirectorySlash Off
RewriteEngine On

# remove access to launcher.php and .htaccess
RewriteCond %{THE_REQUEST} /\.htaccess [NC]
RewriteRule ^ - [F]

# enforce HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# treat existing directories as parameters
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]    

# handle Front Controller for public/
RewriteRule ^(public)/?$ launcher.php?url=$1 [QSA,L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(public/.*?)/?$ launcher.php?url=$1 [QSA,L,NC]

# handle remaining URLs as parameter
RewriteCond %{QUERY_STRING} !(?:^|&)url= [NC]
RewriteRule ^((?!public/).*?)/?$ launcher.php?url=$1 [QSA,L]