簡體   English   中英

.htaccess文件/隱藏子目錄

[英].htaccess file / hide subdirectory

我嘗試了在StackOverflow上找到的幾種解決方案,但是似乎都沒有用。 首先,在文檔根目錄上,我有.htaccess文件index.php ,它們可以檢測語言並重定向到/fr/目錄或/en/目錄。 然后,我有了index.html文件和一個名為“ pages”的目錄,其中所有目錄都屬於我的頁面。 這樣,所有網址都類似於http://example.com/fr/index.htmlhttp://example.com/fr/pages/a.html

我想要 :

  1. 隱藏“ index.html ”。 如果我沒記錯的話,這個解決方案很好:

     RewriteEngine On RewriteRule ^index\\.html$ / [R=301,L] 
  2. 隱藏htmlphp擴展名。 我用了它,它對php很好用,但是對html卻不行。。。不知道為什么。

     RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\\.]+?)/?$ $1.php [NC,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\\.]+?)/?$ $1.html [NC,L] 
  3. 隱藏頁面目錄(我不介意顯示/ fr /或/ en /目錄),以便URL像這樣(最后): http : //example.com/fr/a

這是我的結構

├── fr
|   └── index.html
|   └── pages
|       └──a.html
|       └──b.html etc
├── en
|   └── index.html
|   └── pages
|       └──a.html
|       └──b.html etc
├── .htaccess
└── index.php
  1. 隱藏“ index.html”。 如果我沒記錯的話,這個解決方案很好:

     RewriteEngine On RewriteRule ^index\\.html$ / [R=301,L] 

您說“很好”,但是,這似乎與您的網站結構不相關,因此它似乎無能為力嗎? 該指令假設您在文檔根目錄中有index.html 但是,您是否要刪除index.htmlindex.php無論它們出現在URL路徑中的什么位置? 例如,類似:

RewriteCond %{THE_REQUEST} /index\.(html|php)
RewriteRule (.*)index\.(html|php)$ /$1 [R=301,L]

條件是防止某些服務器配置上的重定向循環。

這還要求index.phpindex.html都包含在DirectoryIndex.

  1. 隱藏html和php擴展名。 我用了它,它對php很好用,但是對html卻不行。。。不知道為什么。

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

這是一個沖突 您具有相同的RewriteRule 模式 -那么哪個應該“獲勝”? 第一條指令將永遠“贏”,第二條指令將永遠不會發生。

但是...您的“網站結構”不完整嗎? 您的站點結構在文檔根目錄中顯示了一個index.php (始終重定向到/ )-子目錄中沒有.php文件,那么為什么需要檢查?

如果在整個站點結構中確實混合了.html.php文件,並且沒有可識別的模式,則需要在重新寫入文件之前執行文件系統檢查以查看文件是否存在(這相對昂貴)。 例如:

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

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

在字符類中使用時,不需要反斜杠轉義點(以匹配文字點),因為在字符類中使用時,它不帶有特殊含義(與正則表達式中的其他地方相反)。

但是,如果有模式 ,則可以大大改善。

  1. 隱藏頁面目錄(我不介意顯示/fr//en/目錄),以便URL像這樣(最后): http://example.com/fr/a : http://example.com/fr/a

為了澄清,您必須首先鏈接到example.com/fr/a

然后,將所有請求重寫到/pages子目錄(並以相同的動作附加.html擴展名),您可以執行以下操作:

RewriteRule ^(fr|en)/([^/.]+)$ /$1/pages/$2.html [L]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM