2017-08-31 27 views
2

我目前有一個靜態的HTML文件託管在我的Wordpress安裝的根文件夾中。我可以訪問網址http://example.com/er_trends_download但當斜線被添加到URL得到一個內部服務器錯誤:http://example.com/er_trends_download/htacess重定向到Wordpress根目錄下的靜態HTML

我已經加入線到.htaccess文件從我的靜態文件,並在WordPress我永久的設置下降的.html也設置爲刪除結尾的斜槓。我沒有保留斜線的偏好,我只是不想讓內部服務器錯誤,無論它是否是URL的一部分。我包括我的全.htaccess文件如下:

# Use PHP5.6 as default 
AddHandler application/x-httpd-php56 .php 

php_value memory_limit 512M 

<IfModule mod_rewrite.c> 
RewriteEngine On 
RewriteBase/
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC] 
RewriteRule^/%1 [NC,L,R] 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule^%{REQUEST_URI}.html [NC,L] 
</IfModule> 


# 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 

回答

0
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule^%{REQUEST_URI}.html [NC,L] 

這段代碼的問題是,當請求中以斜線結尾,則REQUEST_FILENAME服務器變量做(如http://example.com/er_trends_download/。)不包括尾部斜線,但REQUEST_URI變量。所以,你最終重寫爲/er_trends_download/.html(這顯然是錯誤的),但是這會導致重寫循環... /er_trends_download/.html.html/er_trends_download/.html.html.html等等,並且服務器最終打破500錯誤。

儘量不要使用以下:

RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^(.+?)/?$ /$1.html [L] 

這使得尾部的斜槓可選的非貪婪模式.+?確保我們抓住了一切,但不包括結尾的斜線(如果有的話)。這裏不需要NC標誌。

因此,這允許帶和不帶尾隨斜線的URL(可能或不可取)。

+0

謝謝!這樣做的訣竅,我只需將您的代碼建議中的.php更改爲.html 如果您可以在響應中進行此編輯,我會將其標記爲已接受。 – SRouse

+0

不客氣。對不起,是的,'.php'是習慣的力量(現在已更新)! – MrWhite