【发布时间】:2017-10-06 17:46:09
【问题描述】:
我已经建立了一个 Wordpress 网站。 我需要递归删除特定文件夹中文件的 .php 扩展名。
说: www.domain.com/wordpresspage/(设置永久链接,一切都好)。
然后我需要为文件夹递归删除 .php: domain.com/gz/*
domain.com/gz/ 的内容应删除 .php 扩展名。但仅限于此文件夹。
文件夹结构
/
-/images (No .htaccess rules should apply, Wordpress takes care of it.)
-/downloads (No .htaccess rules should apply, Wordpress takes care of it.)
-/gz (.htaccess change, to remove .php file extension, overwriting Wordpress, in this particular folder)
-file1 (No .htaccess rules should apply, Wordpress takes care of it.)
-file2 (No .htaccess rules should apply, Wordpress takes care of it.)
在我使用以下 .htaccess 内容删除 .php 扩展名之前,但现在我只需要在一个文件夹上强制执行此操作,因为 Wordpress 会处理其余部分。
#RewriteBase /
#RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.php\ HTTP
#RewriteRule ^([^.]+)\.php$ https://example.com/$1 [R=301,L]
#RewriteCond %{REQUEST_URI} !(\.[^./]+)$
#RewriteCond %{REQUEST_fileNAME} !-d
#RewriteCond %{REQUEST_fileNAME} !-f
#RewriteRule (.*) $1.php [L]
我已经尝试了很多东西,但最后几个包括:
- 使用旧的 .htaccess 文件删除所有页面上的 .php。这解决了我的路径问题,但在站点的其余部分出现了内部服务器错误。
- 尝试了无数种递归过滤该文件夹的方法,以至于我不再确定它是否可行。 (甚至不记得我尝试过的一小部分)。
- 尝试在永久链接中添加 .php - 但由于它会重定向以再次删除它,我会再次收到内部服务器错误。
TL;DR - 我可以递归删除 .htaccess 文件中特定路径上的 .php,以便 Wordpress 忽略此文件夹并让它处理自己的事情吗?
在第一轮帮助后添加,我的 .htaccess 文件现在看起来像这样:
# -FrontPage-
IndexIgnore .htaccess */.??* *~ *# */HEADER* */README* */_vti*
RewriteEngine On
# Only apply if request starts with /gz/
# followed by at least one character and ends with .php
RewriteCond %{REQUEST_URI} ^/?gz/[^.]+\.php$
# Redirect to the same location but without .php ant the end
RewriteRule ^/?(.*)\.php$ /$1 [R=301,L]
# Only apply if request starts with /gz/ followed by at least one character
RewriteCond %{REQUEST_URI} ^/?gz/[^.]+$
# and the request it not a real directory
RewriteCond %{REQUEST_FILENAME} !-d
# and the request it not a real file
RewriteCond %{REQUEST_FILENAME} !-f
# Rewrite the request with .php at the end
RewriteRule ^(.*)$ /$1.php [L]
<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AddType application/x-httpd-php-old .php
<IfModule security_module>
SecFilterEngine Off
</IfModule>
<IfModule security2_module>
SecRuleRemoveByID 1-99999
SecRuleRemoveByTag unoeuro
</IfModule>
重新应用永久链接后,Wordpress 将此部分添加到 .htaccess 文件中:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
提前致谢,
L
【问题讨论】:
标签: php wordpress apache .htaccess