我是 .htaccess 用法的新手,并試圖通過在線資源學習,但我寫它的規則相互否定,我很難寫出足夠好的 .htaccess 檔案 下面是我當前的 .htaccess 檔案,它適用于某些頁面像洗掉擴展和重寫子域請檢查下面
## Flag for GoDaddy
Options MultiViews
RewriteBase /
## Remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !=f
RewriteRule ^([^\.] )$ $1.php [NC,L]
## Redirect from extensions to non-extensions
RewriteCond %{THE_REQUEST} \s/ (. ?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,NE,L]
## Redirect Pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^post/([a-zA-Z0-9-/] )$ /post.php?ps=$1
RewriteRule ^([a-zA-Z0-9-/] )$ post-files.php?ps=$1 [L,QSA]
## Server Only
## Redirect from www - non-www
RewriteCond %{HTTP_HOST} ^www\.(. )$ [NC]
RewriteRule ^(.*)$ http://$1/$1 [R=301,L]
## SSL Redirect
## RewriteEngine On
## RewriteCond %{HTTPS} ≠On
## RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
## Create Error Pages
ErrorDocument 404 /errors/404.html
ErrorDocument 403 /errors/403.html
ErrorDocument 500 /errors/500.html
## Redirect non-existing pages to index.php
Options SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
上面是當前使用的 .htaccess,我通過 youtube 的教程得到了它,它運行良好,并將 files.php 重定向到僅滿意的檔案,但正如您在此行上方看到的,RewriteRule ^post/([a-zA-Z0-9-/] )$ /post.php?ps=$1它下面的行沒有通過 ps但他們顯示404頁面
我希望結果能夠 domain.com/post-file-slug準確地歸檔domain.com/post-file.php?ps=post-slug-here以獲取規則RewriteRule ^([a-zA-Z0-9-/] )$ post-files.php?ps=$1 [L,QSA]
和
domain.com/post/post-slug-here去確切地domain.com/post.php?ps=post-slug-here為規則
RewriteRule ^post/([a-zA-Z0-9-/] )$ /post.php?ps=$1
我已經為此作業了 2 天,希望能盡快解決。謝謝
uj5u.com熱心網友回復:
## Flag for GoDaddy Options MultiViews
這在什么方面是“GoDaddy 的標志”?啟用 MultiViews 將導致psURL 引數不傳遞給post.php腳本。您需要確保禁用 MultiViews,以便以后的重寫按預期作業。IE。
Options -MultiViews
MultiViews(mod_negotiation 的一部分)本質上支持無擴展 URL。這將導致在處理您的 mod_rewrite 指令之前請求/post/post-slug-here被“重寫” ,因此它永遠不會匹配并且永遠不會重寫請求以包含URL 引數。/post.php/post-slug-here ps
## Remove extensions RewriteEngine On RewriteCond %{REQUEST_FILENAME} !=f RewriteRule ^([^\.] )$ $1.php [NC,L] ## Redirect from extensions to non-extensions RewriteCond %{THE_REQUEST} \s/ (. ?)\.php[\s?] [NC] RewriteRule ^ /%1 [R=301,NE,L]
當前是 MultiViews 允許您的無擴展 URL 作業。上面的第一個條件(RewriteCond指令)不正確。它應該是!-f(不是檔案),而不是!=f(不等于“f” - 始終為真)。但是,這仍然是“錯誤的”,因為您需要.php在重寫請求之前檢查檔案是否存在。如果您只是重寫所有未映射到檔案的請求(這是您在此處嘗試執行的操作),那么稍后將重寫為post.php,post-files.php并且index.php不會按預期進行處理。
\s/ (. ?)\.php[\s?]第二個條件中的正則運算式不是嚴格正確的,因為如果.php在 URL 路徑中省略查詢字串時,它將導致格式錯誤的重定向。例如。在這種情況下,請求/foo?bar.php將導致重定向到/foo?bar根本不應該重定向的時間。正則運算式只需要捕獲 URL 路徑,因此將子模式更改(. ?)為([^?] )。
這兩條規則也是錯誤的。外部重定向應該是第一個。作為一般規則,外部重定向應始終在內部重寫之前進行。
它應該是這樣的:
## Remove extensions
## Redirect to remove ".php" extension
RewriteCond %{THE_REQUEST} \s/ ([^?] ?)\.php[\s?] [NC]
RewriteRule \.php$ /%1 [R=301,NE,L]
# Rewrite to append ".php" extension if corresponding ".php" file exists
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.] )$ $1.php [L]
您應該已經鏈接到沒有.php擴展名的檔案。洗掉.php擴展的重定向僅在更改現有 URL 結構時用于 SEO。
在正則運算式字符類中使用時,無需對文字點進行反斜杠轉義。NC旗幟在這里是多余的。
## Redirect Pages RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^post/([a-zA-Z0-9-/] )$ /post.php?ps=$1 RewriteRule ^([a-zA-Z0-9-/] )$ post-files.php?ps=$1 [L,QSA]
RewriteCond指令僅適用于RewriteRule隨后的第一個指令。那么,上面的第二條規則是無條件處理的——這是故意的嗎?
事實上,這兩個條件可能是多余的。由于正則運算式不包括點,因此正則運算式似乎已經排除了實際檔案。您是否需要能夠直接訪問檔案系統目錄?
字符類[a-zA-Z0-9-/]是“令人困惑的”。最后一個連字符被視為文字連字符(這可能是意圖),但乍一看它可能看起來像一個范圍說明符(如前面在字符類中使用的那樣)。為避免在匹配字符類中的文字連字符時出現混淆,請使用反斜杠對其進行轉義,或者將其移動到字符類中的第一個或最后一個字符。例如。[a-zA-Z0-9/-].
您還缺少L第一條規則中的標志。(您已將其包含在第二個中。)您還需要QSA標志嗎?(即。您是否期望初始請求中包含其他 URL 引數?)
修改了上面的“擴展洗掉”規則,這并不重要,但是這些將請求重寫為post.php并且post-files.php應該真正高于“擴展洗掉”規則的規則。
## Redirect from www - non-www RewriteCond %{HTTP_HOST} ^www\.(. )$ [NC] RewriteRule ^(.*)$ http://$1/$1 [R=301,L]
這個規則是不正確的,并且在錯誤的地方。規范重定向(www 到非 www 和 HTTP 到 HTTPS)通常應該高于其他規則。如上所述,在重寫之前重定向。
但這條規則也是完全不正確的。$1是對 中第一個捕獲的子模式的反向參考RewriteRule,因此http://$1/$1自然會導致格式錯誤的重定向。第一個反向參考應該是%1(到最后匹配的CondPattern)以匹配請求的主機名。通常,您還應該在此處重定向到 HTTPS,而不是 HTTP。例如,規則應為:
:
RewriteRule (.*) https://%1/$1 [R=301,L]
模式周圍的和^是多余的,因為正則運算式默認是貪婪的。$RewriteRule
## SSL Redirect ## RewriteEngine On ## RewriteCond %{HTTPS} ≠On ## RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
雖然注釋掉了,但也是不正確的。它需要在其他重寫之前進行。如果實施 HSTS,它應該放在檔案的頂部,如果沒有,它應該放在檔案的頂部,或者在 www 到非 www 重定向之后(并最小化重定向的數量)。
前面條件中的CondPattern應該是, not (這在兩次計數上完全無效......無效并且比較區分大小寫。將始終為小寫。)!on≠On≠HTTPS
您還缺少R=301andL標志。
RewriteRule 在pattern中不需要捕獲組,因為它沒有在替換字串中使用。^就足夠了(并且效率更高)而不是(.*).
## Create Error Pages ErrorDocument 404 /errors/404.html ErrorDocument 403 /errors/403.html ErrorDocument 500 /errors/500.html
為了便于閱讀,您應該在檔案頂部定義自定義錯誤檔案。(從技術上講,這并不重要。)
## Redirect non-existing pages to index.php Options SymLinksIfOwnerMatch RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
為了便于閱讀,您應該Options在檔案頂部定義一起(使用-MultiViews)。例如:
Options -MultiViews -Indexex SymLinksIfOwnerMatch
(禁用Indexes- 自動生成的目錄串列 - 是個好主意。)
您不需要重復該RewriteEngine指令。(只有這個指令的最后一個實體做任何事情。)把這個規則放在檔案頂部附近,在你的第一個 mod_rewrite 指令之前是合乎邏輯的。(雖然從技術上講,這個指令在檔案中的位置實際上并不重要。)
旁白:您在內部重寫時使用的前綴應該是一致的。在某些規則中,您包含斜杠前綴(例如。/post.php),而在某些規則中,您省略了它(post-files.php)。您已定義RewriteBase /(此處并非嚴格要求,因為它發生) -RewriteBase僅適用于相對替換字串(即,當省略斜杠前綴時)。
更新:
我也有我想在根目錄中排除 404.php 之類的檔案,我如何從重定向中排除某些檔案。當我將 ajax 發送到后端 php 檔案時,它重定向到主頁并且無法檢索資料。
要排除特定檔案,您將在規范重定向之后添加如下規則:
# Exclude "/404.php" from stripping the ".php" extension
RewriteRule ^404\.php$ - [L]
一般來說,一旦你對.php檔案進行無擴展名,你應該到處都是無擴展名的。因此,不應該有意外的重定向。重定向實際上僅適用于 SEO。
關于您的 AJAX 請求,如果您正在發出 POST 請求,那么您可以簡單地將所有 POST 請求排除在進一步處理之外。例如:
# Prevent further processing of POST requests to ".php" files
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule \.php$ - [L]
或者(或同樣),如果您的 AJAX 請求正在設定自定義 HTTP 請求標頭,那么您也可以檢查這一點。
概括
綜合以上幾點,它應該是這樣的:
## Disable MultiViews and Indexes
Options -MultiViews -Indexes SymLinksIfOwnerMatch
## Create Error Pages
ErrorDocument 404 /errors/404.html
ErrorDocument 403 /errors/403.html
ErrorDocument 500 /errors/500.html
RewriteEngine On
RewriteBase /
#### Canonical redirects
## SSL Redirect
## RewriteCond %{HTTPS} !on
## RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
## Redirect from www - non-www
## >>> CHANGE TO "HTTPS://"
RewriteCond %{HTTP_HOST} ^www\.(. ) [NC]
RewriteRule (.*) http://%1/$1 [R=301,L]
#### Rewrite Pages
RewriteRule ^post/([a-zA-Z0-9/-] )$ post.php?ps=$1 [QSA,L]
RewriteRule ^([a-zA-Z0-9/-] )$ post-files.php?ps=$1 [QSA,L]
#### Exceptions
## Exclude "/404.php" from stripping the ".php" extension
RewriteRule ^404\.php$ - [L]
## Prevent further processing of POST requests to ".php" files
RewriteCond %{REQUEST_METHOD} POST [NC]
RewriteRule \.php$ - [L]
#### Remove extensions
## Redirect to remove ".php" extension
RewriteCond %{THE_REQUEST} \s/ ([^?] )\.php[\s?] [NC]
RewriteRule \.php$ /%1 [R=301,NE,L]
## Rewrite to append ".php" extension if corresponding ".php" file exists
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.] )$ $1.php [L]
## Redirect non-existing pages to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/485048.html
標籤:php .htaccess 重定向 改写 openlitespeed
