You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过.htaccess实现斜杠/下划线分隔符的URL重定向

特定URL模式的301重定向解决方案

需求概述

需要匹配以下任意URL模式(variable及后续内容大小写不限):

  • /stays-the-same/variable/*
  • /stays-the-same/variable_*
  • /item-1/stays-the-same/variable_*

将上述URL统一301重定向至:
https://www.example.com/item-1/stays-the-same/variable
其中:

  • variable需转换为全小写
  • 无论原URL是否包含item-1,最终URL必须包含该静态文本
  • variable之后的所有内容(包括末尾的/或_)全部丢弃

补充说明:variable仅由大小写字母或连字符组成。

核心解决方案(修改.htaccess规则)

将以下规则放在.htaccess文件最顶部(RewriteEngine On之后),替换原有的相关重定向规则:

# 强制跳转至https://www.版本
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# 处理目标URL的重定向:提取variable并转小写,确保item-1存在,丢弃后续内容
RewriteCond expr "tolower(%{REQUEST_URI}) =~ m#^/(?:item-1/)?stays-the-same/([a-z-]+)[/_].*$#"
RewriteRule ^(?:item-1/)?stays-the-same/[A-Za-z-]+[/_].*$ https://www.example.com/item-1/stays-the-same/%1 [R=301,L,NE]

规则解释

  1. 强制HTTPS和www规范:先将所有非www、非HTTPS的请求跳转至https://www.开头的地址,确保域名格式统一。
  2. 提取并转换variable:
    • 通过正则匹配提取stays-the-same/之后的variable部分(仅字母和连字符),并通过tolower()转换为小写
    • 不管原URL是否有item-1/前缀,最终跳转的URL都强制加上该静态文本
    • 匹配variable之后的/_开头的任意内容,全部丢弃,只保留小写的variable

完整修改后的.htaccess示例

RewriteEngine On
RewriteBase /

# 新增的核心重定向规则
# 强制跳转至https://www.版本
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# 处理目标URL的重定向:提取variable并转小写,确保item-1存在,丢弃后续内容
RewriteCond expr "tolower(%{REQUEST_URI}) =~ m#^/(?:item-1/)?stays-the-same/([a-z-]+)[/_].*$#"
RewriteRule ^(?:item-1/)?stays-the-same/[A-Za-z-]+[/_].*$ https://www.example.com/item-1/stays-the-same/%1 [R=301,L,NE]

# 原有的其他规则保留
RewriteCond %{HTTP_HOST} ^212\.212\.212\.212$
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

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.

#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} stays-the-same
RewriteRule ^(.*)/$ /$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

注意事项

  • 测试前请清空浏览器缓存,避免旧重定向规则干扰
  • 确保服务器开启了mod_rewrite模块
  • 如果使用其他域名,替换规则中的example.com为实际域名

内容的提问来源于stack exchange,提问作者Nick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 11:20:30