我已經搜索了這個場景的高低,但不斷遇到更簡單的解決方案
例如
RewriteRule ^stays-the-same/[^A-Z]*[A-Z] %1 [R=301,L,NE]
目標是查看 url 是否適合這些可能性中的任何一種
/stays-the-same/variable/*
/stays-the-same/variable_*
/item-1/stays-the-same/variable_*
/item-1/stays-the-same/variable_*
無論變數的大小寫或它之后的任何內容
為它到 301 到小寫版本/item-1/stays-the-same/variable- 并且總是到https://www.版本。
星號表示任何內容,例如多條路徑、數字、下劃線等
任何建議都非常感謝。
編輯
"variable"只是字母或連字符,大寫或小寫。
"item-1"是靜態文本,應始終位于結尾 URL 中,即使它不像前兩個示例中那樣存在。
變數之后的所有內容都被丟棄,任何尾隨/或_。
編輯 2 - 完整.htaccess檔案
RewriteEngine On
RewriteBase /
RewriteCond expr "tolower(%{REQUEST_URI}) =~ m#^((/[^/] )?/[^/] /[a-z-] )[/_]#"
RewriteCond %{HTTP_HOST}@%1 ^(?:www\.)?(. ?)\.?@(. )
RewriteRule ^(item-1/)?stays-the-same/([a-z-]*[A-Z][a-z-]*)[/_] https://www.%1%2 [R=301,L]
RewriteCond %{HTTP_HOST} ^212\.212\.212\.212$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond expr "tolower(%{REQUEST_URI}) =~ /(.*)/"
RewriteRule ^stays-the-same/[^A-Z]*[A-Z] %1 [R=301,L,NE]
RewriteRule \.(jpe?g|png|gif|ico|bmp|pdf|docx?|txt|css|js)$ - [L,NC]
RewriteRule ^([^\s ] )(?:\s| ) ([^\s ] )((?:\s| ) .*)$ $1-$2$3 [N,DPI]
RewriteRule ^([^\s ] )(?:\s| ) (.*)$ /$1-$2 [L,R=301,DPI]
RewriteRule ^signup/ https://www.example.com/signup [R=301,L]
RewriteRule ^word/ https://www.example.com/other-word [R=301,L]
RewriteCond %{REQUEST_URI} !pagespeed
RewriteCond %{QUERY_STRING} ^PageSpeed=noscript$ [NC]
RewriteRule .* %{REQUEST_URI}? [L,R=301]
Header set Content-Language "en"
RewriteCond %{QUERY_STRING} (^|&)option\=com_flag($|&)
RewriteCond %{QUERY_STRING} (^|&)view\=inventory($|&)
RewriteCond %{QUERY_STRING} (^|&)ajax\=true($|&)
RewriteCond %{QUERY_STRING} (^|&)country($|&)
RewriteRule ^index\.php$ /stays-the-same? [L,R=301]
## No directory listings
IndexIgnore *
## Can be commented out if causes errors, see notes above.
Options FollowSymlinks
Options -Indexes
## Mod_rewrite in use.
RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} stays-the-same
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site then comment out the operations listed
# below by adding a # to the beginning of the line.
# This attempts to block the most common type of exploit `attempts` on Joomla!
#
# Block any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|<)([^s]*s) cript.*(>|>) [NC,OR]
# Block any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.
## Begin - Custom redirects
# 301 --- https://www.example.com/?ref=producthunt => https://www.example.com
RewriteCond %{QUERY_STRING} (^|&)ref\=producthunt($|&)
RewriteRule ^$ /? [L,R=301]
# 301 --- https://www.example.com/signup?view=item-2 => https://www.example.com/signup
RewriteCond %{QUERY_STRING} (^|&)view\=item-2($|&)
RewriteRule ^signup$ /signup? [L,R=301]
RewriteRule ^stay-same/(.*)$ /shoes/$1 [QSA,R=301,L]
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects
##
# Uncomment the following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##
# RewriteBase /
## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.
php_value upload_max_filesize 5M
php_value post_max_size 10M
php_value memory_limit 320M
uj5u.com熱心網友回復:
更新:對規則/條件進行了幾處更改,以允許全小寫變數/item-1并在生成的 URL 路徑上強制使用前綴。丟棄變數后的 URL 路徑部分-總是丟棄一些東西。
嘗試以下方法(需要 Apache 2.4):
RewriteCond expr "tolower(%{REQUEST_URI}) =~ m#^(?:/item-1)?/([^/] /[a-z-] )[/_]#"
RewriteCond %{HTTP_HOST}@%1 ^(?:www\.)?(. ?)\.?@(. )
RewriteRule ^(item-1/)?stays-the-same/([a-z-] )[/_] https://www.%1/item-1/%2 [NC,R=301,L]
該RewriteRule 模式確定請求的 URL 路徑是否與可能的 URL 之一匹配,并且是否variable僅包含字母或連字符,并且這些字母中至少有一個是大寫的。該NC標志確保這是一個不區分大小寫的匹配,因此變數可以是大寫/小寫。
然后第一個條件將 URL 路徑轉換為小寫。小寫 URL 路徑的直到并包括variableonly 的部分被捕獲(不包括可選前綴),它被傳遞給反向參考中/item-1的以下條件%1。
然后,第二個條件提取主機名的一部分,減去www.存盤在%1反向參考中的前綴(如果有)(再次),并將小寫的 URL 路徑(從第一個條件)傳遞給反向參考中的以下規則%2。
請注意,替換字串(表示 URL 路徑)中的%2反向參考現在不包括斜杠前綴,因此斜杠現在包含在替換字串中 - 我認為這比將斜杠作為反向參考的一部分更易讀(如以前是這樣)。
/item-1前綴需要在替換字串中硬編碼,因為這在請求的 URL 路徑中是可選的。
您可以簡化這一點并硬編碼www 主機名并避免第二個條件。例如:
RewriteCond expr "tolower(%{REQUEST_URI}) =~ m#^(?:/item-1)?/([^/] /[a-z-] )[/_]#"
RewriteRule ^(item-1/)?stays-the-same/([a-z-] )[/_] https://www.example.com/item-1/%1 [NC,R=301,L]
這直接使用替換字串%1中第一個條件的反向參考。
首先使用 302(臨時)重定向進行測驗,以避免潛在的快取問題,并確保在測驗前清除瀏覽器快取。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/510598.html
上一篇:XAMP/Apache-htdocs中的檔案夾作為根?
下一篇:Linux上的ASP.NETCore6ReactJSWeb應用程式存在IDX10634問題:無法創建SignatureProvider
