2017-10-06 65 views
2

我已經建立了一個Wordpress網站。 我需要遞歸刪除特定文件夾中文件的.php擴展名。遞歸刪除.php .htaccess和Wordpress

說: 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] 

我試過一噸的事情了,但過去幾年包括:

  1. 使用舊的.htaccess文件,消除.PHP上的所有網頁。這解決了我的路徑問題,但在網站的其他部分發生內部服務器錯誤。
  2. 試圖通過遞歸過濾出該文件夾的巨大方法,直到我不再確定它是否可能。 (甚至不記得我試過的一小部分)。
  3. 嘗試在永久鏈接添加.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 

由於提前,

大號

回答

2

你可以做的是以下

RewriteEngine On 
# Only apply if no rewrite 
RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
# 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] 

所以,如果你要求例如/gz/gz5.php將會被重定向到/ gz/gz5 ,如果請求是/ gz/gz5,它會重新定位到/gz/gz5.php