2016-01-23 62 views
2

我一直在這個網站上工作了很長時間。我使用.htaccess文件來獲得乾淨的URL。這種精細的工作我個人的測試服務器,這是HTTP,並在我的本地,但移動到生產服務器(與啓用HTTPS和工作)以後,一些規則沒有工作除非第一個字母大寫,否則Mod_rewrite不適用於某些規則? HTTPS服務器

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteRule ^index/?$ index.php [NC,L] 
    RewriteRule ^home/?$ index.php [NC,L] 
    RewriteRule ^main/?$ index.php [NC,L] 
    RewriteRule ^about/?$ about.php [NC,L] 
    RewriteRule ^music/?$ music.php [NC,L] 
    RewriteRule ^shows/?$ shows.php [NC,L] 
    RewriteRule ^blog/?$ blog.php?page=1 [NC,L] 
    RewriteRule ^blog/([\0-9]+)/?$ blog.php?page=$1 [NC,L] 
    RewriteRule ^contact/?$ contact.php [NC,L] 
    RewriteRule ^profile/?$ profile.php [NC,L] 
    RewriteRule ^manage-site/?$ manage-site.php [NC,L] 
    RewriteRule ^my-blog-posts/?$ my-blog-posts.php [NC,L] 
    RewriteRule ^new-blog-post/?$ new-blog-post.php [NC,L] 
    RewriteRule ^edit-music/?$ edit-music.php [NC,L] 
    RewriteRule ^admin-login/?$ admin-login.php [NC,L] 
    RewriteRule ^admin/?$ admin-login.php [NC,L] 
    RewriteRule ^forgot-password/?$ forgot-password.php [NC,L] 
    RewriteRule ^password-reset/?$ password-reset.php [NC,L] 


    RewriteRule ^blog/article/([\w-]+)/?$ blog-post.php?slug=$1 [NC,L] #handle requests for Individual Blog Posts 
    RewriteRule ^blog/article/([\w-]+)/([^/\.]+)/?$ blog-post.php?slug=$1&reply-comment=$2 [NC,L] #handle requests for Individual Blog Posts with a comment specified for replies when JavaScript is disabled 
    RewriteRule ^password-reset/([^/]+)/?$ password-reset.php?token=$1 [NC,L] #handle requests for password-reset.php with the token included in a nicer-looking url 
</IfModule> 

ErrorDocument 404 /404.php 

基本上任何有一個連字符的規則,即管理網站,給了我一個404錯誤,除非我使用它,即管理網站 - 這很好。

任何想法?

+0

在該片段中沒有看到解釋連字符依賴性或區分大小寫的任何內容。可能404在RewriteRule執行之前(或之後)發生。錯誤日誌通常會爲404提供附加信息。重寫日誌記錄還可能提供更多線索: RewriteLog日誌/ rewrite.log RewriteLogLevel 9 –

+0

看起來它實際上是多個頁面,而不僅僅是連字符。原本,我只注意到帶連字符的人。幸運的是,網站上的每一個鏈接/重定向實際上都是大寫的,因爲我覺得它很美觀。儘管如此,仍然很奇怪。 我試圖刪除ErrorDocument行,但這並沒有幫助,除了改回默認的Apache 404頁面。 無效的命令「RewriteLog」,也許是拼寫錯誤或定義的不包括在服務器配置的模塊 ^沒有運氣的rewritelog – Chris

+0

我也注意到,但是:週五23年1月22日:55:32.996435 2016年] [協商:錯誤] [pid 22241] [client 47.55.91.133:52285] AH00687:協商:發現匹配請求的文件:/ var/www/html/admin-login(無可協商)。 所以如果這是Apache 2.4,重寫日誌記錄與此處記錄的有所不同,那麼重寫規則只是簡單地不會引起admin-login,它應該是重寫它到admin-login.php – Chris

回答

2

如果對此目錄啓用MultiViews(mod_negotiation),可能會發生這種情況。嘗試通過添加以下到你的.htaccess文件的頂部禁用MultiViews:(即導致404)

Options -MultiViews 

至於代碼中的問題,在此可能會失敗的情形是,當你被請求的URL 尾隨斜線一個PHP文件具有相同基名AcceptPathInfoOff存在。例如example.com/main/否則應改寫爲/main.php,而是被改寫爲/main.php/(由mod_negotiation模塊),這導致404

MultiViews通常默認在許多服務器上啓用,然而,一個默認的Apache安裝不應該有這個啓用了的盒子。

啓用MutliViews後,Apache會嘗試通過測試各種文件擴展名(這將返回適當的MIME類型)來將不存在的文件與文件系統上的文件進行映射。例如。要求/main(不存在),它會嘗試/main.php - 成功。但是,這在 mod_rewrite之前運行,所以如果MultiViews開始運行,mod_rewrite規則將永遠不匹配。

在上面提到的場景中,/main/也會觸發mod_negotiation,在內部將請求重寫爲/main.php/(尾部的斜線仍在尾部)。但是,如果AcceptPathInfo爲Off,則會觸發404。這可以防止mod_rewrite重寫URL。

通過資本的要求,MultiViews失敗(我假設你是在一個大小寫敏感的操作系統,例如,Linux呢?),但因爲你有NC標誌的RewriteRule現在這個工作。

+0

無論是否使用斜線都無法使用,但工作正常!非常感謝。我確實在Ubuntu 14.04上使用LAMP堆棧。我不能說我對現在發生的事情瞭解得很多,但我應該更多地瞭解自己的情況。謝謝! – Chris

+0

當它「失敗」時,我認爲這是404?確切的行爲可能取決於其他設置和文件系統的結構。例如,如果你有一個同名的目錄,那麼mod_dir很可能會在mod_negotiation之前(和阻止)並自動追加一個斜線。但是,如果您在重寫可能與文件系統衝突的請求時遇到問題,那麼它幾乎總是要麼是MultiViews,要麼是mod_dir(DirectorySlash),這是原因。 – MrWhite