htaccess - 使用过滤器更改文件目录

htaccess - change file dir with filter

如果请求 uri 不包含 api 或列表,我目前正在尝试制作 htaccess 以在新/中查找文件。我试图做这样的事情,但它显示 403 错误。有没有更简单的过滤方法,或者?

RewriteEngine on 
RewriteCond %{REQUEST_URI} /api/ [NC]
RewriteCond %{REQUEST_URI} /list/ [NC]
RewriteRule ^/(.*)$/ new/ [L, R]

完整的 htaccess 代码

#remove html file extension
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ .html [NC,L]

#remove php file extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ .php [NC,L] 

#set max post size
php_value memory_limit 2048M
php_value max_execution_time 3000
php_value max_input_time 6000
php_value post_max_size 800M
php_value upload_max_filesize 200M

#Caching schema
<FilesMatch "\.(png|svg|css|eot|ttf|woff|woff2|otf|js|css)$">
Header set Cache-Control "public, max-age=86400000"
</FilesMatch>

DirectoryIndex index.html

#Custom 404 errors
ErrorDocument 404 /404.html

#Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

<Files error_log>
order allow,deny
deny from all
</Files>

#Prevent directory listings
Options All -Indexes

编辑: 使用 OP 的完整 htaccess 文件,请尝试以下操作。

#remove html file extension
RewriteEngine on 
##For external redirect rule.
RewriteCond %{THE_REQUEST} !\s/(?:api|list).*\s [NC]
RewriteRule ^(.*)/?$ new/ [L,R=301]

##For internal redirect rule.
RewriteRule ^new/(.*)/?$  [L,NC]


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/?$ .html [NC,L]

#remove php file extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/?$ .php [QSA,NC,L] 

#set max post size
php_value memory_limit 2048M
php_value max_execution_time 3000
php_value max_input_time 6000
php_value post_max_size 800M
php_value upload_max_filesize 200M

#Caching schema
<FilesMatch "\.(png|svg|css|eot|ttf|woff|woff2|otf|js|css)$">
Header set Cache-Control "public, max-age=86400000"
</FilesMatch>

DirectoryIndex index.html

#Custom 404 errors
ErrorDocument 404 /404.html

#Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all
</Files>

<Files error_log>
order allow,deny
deny from all
</Files>

#Prevent directory listings
Options All -Indexes


根据您的显示 attempts/rules,请您尝试关注一次。请确保在测试您的网址之前清除浏览器缓存。

RewriteEngine on 
##For external redirect rule.
RewriteCond %{REQUEST_URI} !^/(?:api|list)/? [NC]
RewriteRule ^(.*)/?$ new/ [L,R=301]

##For internal redirect rule.
RewriteRule ^new/(.*)/?$  [L,NC]

或尝试以下规则集:请确保一次只尝试 1 组,高于或低于。

RewriteEngine on 
##For external redirect rule.
RewriteCond %{THE_REQUEST} !\s/(?:api|list).*\s [NC]
RewriteRule ^(.*)/?$ new/ [L,R=301]

##For internal redirect rule.
RewriteRule ^new/(.*)/?$  [L,NC]

您可以在站点根目录 .htaccess 中尝试这些规则:

RewriteEngine On 

# redirect to /new if not /api, /list, /new and file exists in /new
RewriteCond %{THE_REQUEST} !\s/+(new|api|list)/ [NC]
RewriteCond %{DOCUMENT_ROOT}/new/[=10=] -f
RewriteRule .* /new/[=10=] [L,R=301,NE]

## remaining rules go below this line ##

# remove html file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ .html [L]

# remove php file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ .php [L] 

确保在清除浏览器缓存或从新浏览器进行测试后对其进行测试。