2017-02-09 43 views
1

我試圖從URL中隱藏一個子目錄.htaccess。我有一個PHP腳本,每當客戶進入網站時執行。它是根目錄Apache目錄中的index.php。該腳本確定要在網站中使用的語言並重定向到目標目錄。英語是我的默認語言,所以我需要「en」目錄隱藏在URL中,同時將根目錄中的所有URL請求重定向到「en」文件夾,以便它們不會生成404 HTTP錯誤。我部分地與以下行來實現這一點:.htaccess總是隱藏URL中的某個子目錄

#Remove en/ directory from URL 
RewriteRule ^$ en/ 

#Forward all the requests to en/ directory 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ en/$1 

當請求轉發到「EN」目錄,這是在URL中再次顯示。這將是流動:

如何刪除「en」目錄在列表中的最後一個操作之後離開URL http://domain.com/panel?有沒有更好的方法來管理這種行爲?

目前的規定:

Options -Indexes +FollowSymlinks -MultiViews 

ErrorDocument 404 /404.php 

RewriteEngine On 

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

#Prevent direct access to PHP Scripts 
RewriteCond %{ENV:REDIRECT_STATUS} ^$ 
RewriteRule \.php$ - [R=404,L] 

#Remove WWW 
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] 
RewriteRule ^(.*)$ http://%1/$1 [R=301,L] 

回答

1

有這樣說:

Options -Indexes +FollowSymlinks -MultiViews 
ErrorDocument 404 /404.php 

RewriteEngine On 

#Remove WWW 
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
RewriteRule^http://%1%{REQUEST_URI} [R=301,L,NE] 

# add a trailing slash if public/$1 is a directory 
RewriteCond %{DOCUMENT_ROOT}/en/$1 -d 
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L] 

#Prevent direct access to PHP Scripts 
RewriteCond %{THE_REQUEST} \.php[?/\s] [NC] 
RewriteRule^- [R=404,L] 

RewriteRule ^/?$ en/ [L] 

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

清除瀏覽器緩存和測試流程。

+0

我得到了和以前一樣的行爲。注意:.htaccess文件位於根目錄中。 – ProtectedVoid

+0

剛剛添加完整的.htaccess – ProtectedVoid

+0

我收到了同樣的結果。我確定我清除了緩存。額外的說明:當試圖訪問不是「en」的其他目錄時,我得到404錯誤。例如:http://domain.com/es給出404錯誤(「es」目錄與「en」存在相同) – ProtectedVoid