问题原文
原帖链接 · 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=210tohttps://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.
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
