大网数据广告活动
网站运营与故障问答

Use RedirectMatch in htaccess for many URLs

· 52CE 我爱测

I need to redirect: https://example.com/thispath/beach?start=20 https://example.com/thispath/beach?start=40 https://example.com/thispath/beach?start=60 etc... to: https://example.com/thispath/beach there are other URLs such as : …

问题原文

原帖链接 · Goble · CC BY-SA 4.0

I need to redirect:

https://example.com/thispath/beach?start=20
https://example.com/thispath/beach?start=40
https://example.com/thispath/beach?start=60

etc...

to:

https://example.com/thispath/beach

there are other URLs such as :

https://example.com/thispath/sunset?start=20
https://example.com/thispath/sunset?start=40
https://example.com/thispath/sunset?start=60
etc....

which will need to be redirected to :

https://example.com/thispath/sunset

and

    https://example.com/menu-alias/content/cat-alias/?start=210
https://example.com/menu-alias/content/cat-alias/?start=105

to

https://example.com/menu-alias/

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

You can't use a mod_alias RedirectMatch here because RedirectMatch matches against the requested URL-path only, not the query string. To match the query string you need to use mod_rewrite (RewriteRule and RewriteCond directives) and match against the QUERY_STRING server variable in a condition.

For example, to redirect /thispath/beach?start=<number> you can do something like the following near the top of your root .htaccess file:

RewriteEngine On

RewriteCond %{QUERY_STRING} ^start=\d+$
RewriteRule ^thispath/beach$ /$0 [QSD,R=301,L]

Note that this matches ?start=<number> (case-sensitive) as in your examples, it does not match ?start= or ?start=abc or ?foo=1&start=<number> or ?Start=<Number>.

The QSD flag (Apache 2.4) discards the query string from the request.

$0 is a backreference that contains the entire match from the RewriteRule pattern, so it basically redirects to the same URL-path, just less the query string.

You can modify the above rule to match /thispath/sunset?start=<number> as well by using alternation in the RewriteRule pattern. For example:

:
RewriteRule ^thispath/(?:beach|sunset)$ /$0 [QSD,R=301,L]

The ?: prefix inside the parenthesised group makes it non-capturing.

https://example.com/menu-alias/content/cat-alias/?start=210 to https://example.com/menu-alias/

This is a little different and is better served with an additional rule. For example:

RewriteCond %{QUERY_STRING} ^start=\d+$
RewriteRule ^(menu-alias/)content/cat-alias/$ /$1 [QSD,R=301,L]

The $1 backreference matches the first capturing group from the RewriteRule pattern. ie. menu-alias/ in this case.

NB: Test first with 302 (temporary) redirects to avoid potential caching issues.

Aside: From your earlier question it looks like this URL parameter might be used by your website (as part of a photo gallery perhaps)? If this is the case then redirection is not the correct action. (The answer on that question raised this same concern but received no response.)


UPDATE: For example.com/menu-alias/content/cat-alias/?start=210 etc... I want to throw a 404 error instead.

To trigger a 404 instead change the RewriteRule directive to read:

:
RewriteRule ^menu-alias/content/cat-alias/$ - [R=404]

No susbstitution string is required (denoted by the - argument). The L flag is not required when used with a non-3xx return status; it is implied.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-08-26)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。