K
K
Ken Jee2019-08-30 19:21:32
Apache HTTP Server
Ken Jee, 2019-08-30 19:21:32

What should be the regular expression for RewriteRule to rewrite a specific GET parameter?

There is a URL like /news?page=3&utm=ex
Task: redirect all similar URLs to /news/page-3?utm=ex
In this case, the page parameter can be placed anywhere, i.e. it is not necessary to be the first after the question mark and of course it can be the only parameter, or it can be one of the set ... Tell me
how the RewriteRule rule is formed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2019-08-30
@Machez

If you need to remove only page from %{QUERY_STRING}, and leave the rest of the parameters as they are.

RewriteCond &%{QUERY_STRING} ^(.*?)&page=([^&]+)(.*)
RewriteCond /$1/page-%2?%1%3 ^(.+?\?)&*(.*)
RewriteRule ^(news)$ %1%2 [R=301,L]

Another option.
RewriteCond %{QUERY_STRING} ^()page=([^&]+)&*(.*)  [OR]
RewriteCond %{QUERY_STRING} ^(.*?)&page=([^&]+)(.*)
RewriteRule ^(news)$ /$1/page-%2?%1%3 [R=301,L]

If you rely on the fact that Apache itself will remove the extra character &at the end of the line ?utm=ex&, then it will do.
RewriteCond %{QUERY_STRING} ^(|.*?&)page=([^&]+)&*(.*)
RewriteRule ^(news)$ /$1/page-%2?%1%3 [R=301,L]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question