簡體   English   中英

特定文件的mod_rewrite異常

[英]mod_rewrite exception for a specific file

我的頁面沒有重定向,因為我的.htaccess文件設置為:

RewriteEngine on  
RewriteCond $1 !^(index\.php|resources|robots\.txt)  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*)$ index.php/$1 [L,QSA]   

我將這個設置用於我的MVC框架,所以我得到像/controller/method/argument這樣的網址,但是當我重定向到/forum/login.php時它切換到/ forum /。

如何將此作為例外添加,以便我能夠重定向到/forum/login.php

我發現我的/ forum /目錄中的另一個.htaccess也可能導致問題?

# BEGIN PunBB

<IfModule mod_rewrite.c>
    # MultiViews interfers with proper rewriting
    Options -MultiViews

    RewriteEngine On

    # Uncomment and properly set the RewriteBase if the rewrite rules are not working properly
    #RewriteBase /

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . rewrite.php [L]
</IfModule>

首先,我會告訴你如何閱讀你的RewriteRule:

您從第一個(或下一個)RewriteRule條目開始:

RewriteRule ^(.*)$ index.php/$1 [L,QSA]

第一個參數是可以匹配您請求的URL的正則表達式。 ^(.*)$匹配所有內容,並將此“所有內容”存儲在稍后可以使用的變量中。

只有當前有RewriteCond條目時,才會對它們進行評估:

RewriteCond $1 !^(index\\.php|resources|robots\\.txt)

$1是對RewriteRule的第一個括號內匹配的內容的引用。 這與第二個參數進行比較,第二個參數是一個表示幾個顯式名稱的正則表達式,並且! 否定表達式,例如,只有當正則表達式不匹配時,此規則才允許執行RewriteRule。 如果此條件返回true,則將查看下一個條件。

RewriteCond %{REQUEST_FILENAME} !-f

如果請求的文件名不是硬盤上的真實文件,則此條件為真。

RewriteCond %{REQUEST_FILENAME} !-d

如果請求的文件名不是真實目錄,則此條件為真。

只有當所有這些條件都成立時(它們與AND鏈接在一起),我們才會回到重寫規則:

RewriteRule ^(.*)$ index.php/$1 [L,QSA]

該重寫步驟的結果被定義為第二和第三參數。 再次使用$1作為匹配的內容,並且參數定義此規則(如果最初匹配)將是最后一個規則(L),並且重寫目標中定義的任何查詢字符串將附加到任何查詢字符串在原始網址(QSA)中。

批判:

MVC框架的通常重寫嘗試盡可能高效。 您的重寫條件都必須進行評估才能成功重寫。 只有當任何RewriteCond返回false時,才會停止。 每個被重寫的請求都需要進行大量的cpu密集測試。 首先是RewriteRule正則表達式,然后是第一個RewriteCond中的正則表達式,然后對文件系統進行兩次硬盤測試,以確定是否存在文件。

另一方面,第一個RewriteCond似乎是不必要的。 它測試某些名稱,如果找到,則中止重寫。 “index.php”應該被第二個RewriteCond檢測到,因為它是一個現有文件(如果不是,那么重寫將如何工作)。 任何以“資源”開頭的東西也會匹配,但可能不應該出於同樣的原因:第二個RewriteCond會找到現有資源。 最后是“robots.txt”文件。 如果你想在機器人獲取你的網站時避免使用404,那么擁有一個可能是一個好主意。

由於您沒有更改查詢字符串中的任何內容,因此不需要[QSA]指令。

改進:

RewriteEngine on  
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]  
RewriteRule ^.*$ index.php [L]

第一個RewriteRule將匹配整個請求的路徑。 兩個RewriteCond與[OR]連接,因此返回true的第一個RewriteCond將取消進一步的評估。 第一個RewriteCond測試所請求的文件是否存在。 如果存在,則返回true,處理返回到第一個RewriteRule。 目標表達式為“ - ”,表示“不重寫”。 [L]停止進一步處理重寫規則。 所以最后,對於現有文件,我們只有一個正則表達式和一個文件系統測試,之后,這個現有文件將被發送到瀏覽器。

如果沒有找到文件,第一個RewriteRule和RewriteCond將不會觸發,因此[L]不會停止該過程。 所以第二個RewriteRule被執行了。 這個是無條件的,正則表達式與之前相同,匹配所有內容,並將其重寫為“index.php”。

如果存在任何文件,則此重寫不會調用index.php,包括/forum/login.php。

如果你想繼續解析$_SERVER['PATH_INFO']而不是$_SERVER['REQUEST_URI']你可以將第二個更改為RewriteRule ^.*$ index.php/$0 [L] $_SERVER['REQUEST_URI']

試試這個:

RewriteEngine on  
RewriteCond $1 !^(index\.php|forum|resources|robots\.txt)  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*)$ index.php/$1 [L,QSA]   

還有這個:

# BEGIN PunBB

# ----------------------------------------------------------------------
# Start rewrite engine
# ----------------------------------------------------------------------

<IfModule mod_rewrite.c>
    # MultiViews interfers with proper rewriting
    Options -MultiViews

    RewriteEngine On

    # Uncomment and properly set the RewriteBase if the rewrite rules are not working properly
    RewriteBase /forum/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . rewrite.php [L]
</IfModule>


# ----------------------------------------------------------------------
# Better website experience for IE users
# ----------------------------------------------------------------------

# Force the latest IE version, in various cases when it may fall back to IE7 mode
# github.com/rails/rails/commit/123eb25#commitcomment-118920
# Use ChromeFrame if it's installed for a better experience for the poor IE folk

<IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
        BrowserMatch MSIE ie
        Header set X-UA-Compatible "IE=Edge,chrome=1" env=ie
    </IfModule>
</IfModule>

<IfModule mod_headers.c>
    # Because X-UA-Compatible isn't sent to non-IE (to save header bytes),
    # We need to inform proxies that content changes based on UA
    Header append Vary User-Agent
    # Cache control is set only if mod_headers is enabled, so that's unncessary to declare
</IfModule>


# ----------------------------------------------------------------------
# UTF-8 encoding
# ----------------------------------------------------------------------

# Use UTF-8 encoding for anything served text/plain or text/html
AddDefaultCharset utf-8

# Force UTF-8 for a number of file formats
AddCharset utf-8 .html .css .js .xml .json .rss


# ----------------------------------------------------------------------
# A little more security
# ----------------------------------------------------------------------

# Do we want to advertise the exact version number of Apache we're running?
# Probably not.
## This can only be enabled if used in httpd.conf - It will not work in .htaccess
# ServerTokens Prod


# "-Indexes" will have Apache block users from browsing folders without a default document
# Usually you should leave this activated, because you shouldn't allow everybody to surf through
# every folder on your server (which includes rather private places like CMS system folders).
<IfModule mod_autoindex.c>
    Options -Indexes
</IfModule>

# END PunBB

暫無
暫無

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

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