.htaccess 使用htaccess重定向,使用斜杠或下划线作为分隔符

hc8w905p  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(124)

我已经为这个场景四处寻找,但不断遇到更简单的解决方案
例如,

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%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1-$2$3 [N,DPI]
RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$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} (<|%3C)([^s]*s)+cript.*(>|%3E) [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
d8tt03nd

d8tt03nd1#

**UPDATE:**对规则/条件进行了几处更改,以允许使用全部小写的 variable,并在生成的URL路径上强制使用/item-1前缀。variable 后面的URL路径部分将被丢弃-始终丢弃 something

请尝试以下操作(需要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标志确保这是不区分大小写的匹配,因此 * 变量 * 可以是大写/小写。
第一个 condition 会将URL路径转换成小写。只会撷取variable之前的小写URL路径部分(不包括选择性的/item-1前置词),并在%1反向指涉中传递给后面的 condition
然后,第二个 condition 提取主机名的一部分,减去(再次)存储在%1反向引用中的www.前缀(如果有),* 并将小写URL路径(来自第一个 condition)传递到%2反向引用中的以下规则。
请注意,substitution 字符串(表示URL路径)中的%2反向引用现在排除了斜杠前缀,因此斜杠现在包含在 substitution 字符串中-我认为这比将斜杠作为反向引用的一部分(如以前那样)更具可读性。
/item-1前缀需要在 substitution 字符串中硬编码,因为这在请求的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]

这会直接从 substitution 字串中的第一个条件使用反向指涉%1
首先使用302(临时)重定向进行测试,以避免潜在的缓存问题,并确保在测试之前清除浏览器缓存。

相关问题