2016-12-07 51 views
1

處理錯誤頁面我有一個網站,那裏有很多目錄,如test所以當我打開我的網站domain.com/test它給了我domain.com/test/我想這最後的斜線被刪除刪除最後一個斜線和擴展並且還通過htaccess的

我也想這樣,當我打開像page.php 頁它給了我domain.com/page

除了去除進一步擴展,我想所有的錯誤頁面發送給domain.com/error404 我也已經添加代碼非httphttps

這是我目前的嘗試:

RewriteEngine On 
RewriteBase/
Options All -Indexes 

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L] 

ErrorDocument 403 /www.domain.com/error404.php 
ErrorDocument 404 /https://www.domain.com/error404.php 

HTTPS工作正確,但我也想刪除最後的斜槓和擴展名,並處理目前不工作的錯誤頁面。

+0

從指向目錄的URL中刪除尾部斜槓是一個安全問題。 – anubhava

+0

@anubhava請爲這些東西建議我一些東西,我不想擴展名,不想顯示index.php文件以及錯誤頁面error404.php。 – LIsa

+0

好的檢查下面的答案。 – anubhava

回答

0

你可以在您的網站根目錄中使用這些規則.htaccess:

ErrorDocument 403 /error404.php 
ErrorDocument 404 /error404.php 

RewriteEngine On 

# redirect to https 
RewriteCond %{REQUEST_METHOD} !POST 
RewriteCond %{SERVER_PORT} 80 
RewriteRule^https://www.domain.com%{REQUEST_URI} [R=301,L,NE] 

# remove index.php 
RewriteCond %{REQUEST_METHOD} !POST 
RewriteCond %{THE_REQUEST} /index\.php [NC] 
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC] 
RewriteRule^%1 [L,R=301,NE] 

# To externally redirect /dir/file.php to /dir/file 
RewriteCond %{REQUEST_METHOD} !POST 
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC] 
RewriteRule^/%1 [R=301,NE,L] 

# To internally forward /dir/file to /dir/file.php 
RewriteCond %{DOCUMENT_ROOT}/$1.php -f 
RewriteRule ^(.+?)/?$ $1.php [L] 
+0

先生,我不想在URL中顯示任何索引。現在我是它像domain.com/index/ 並且圖像也不加載,因爲網站中沒有索引文件夾。 可以從網址隱藏索引使其像domain.com或domain.com/folder-name – LIsa

+0

我認爲提交表單時發佈數據存在問題.. PLease更新這個先生,表單發佈數據有一些問題。 。 – LIsa

+0

ok在POST數據的重定向規則中添加了一個條件。 – anubhava

0

您可以打開目錄的斜槓使用:

DirectorySlash Off 

,也可以刪除與trailling斜線:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.+?)/$ /$1 [R=302,NE,L] 

和重定向錯誤可以是:

ErrorDocument 404 /path/to/error404