URL Rewrite from Quary

Hi, I need help to rewrite URL using the Cloudflare rules.
Example I have some pages that can be visit by entering the following URL on browser.

 domain.com/movie?q=abc
 domain.com/movie?q=def
 domain.com/series?q=ghi
 domain.com/series?q=jkl

and I want to rewrite all of those URL as like they can be visited by entering the following URL on browser.

 domain.com/movie/abc
 domain.com/movie/def
 domain.com/series/ghi
 domain.com/series/jkl

How can I do that? I’m a bit new in Cloudflare. So, if any of you can give me easy example or tutorial then that will be helpful for me.

Thank you

Just do a dynamic rewrite for the URI path with:
concat(http.request.uri.path,"/",http.request.uri.query)
That should give you what you want (may or may not need the "/", in there, give it a try first and see).

Hi, thank you for your reply. I did mention early that I’m new in Cloudflare. I did exactly you suggest me to do. But I don’t get what I want. Here is the screenshot what I’m done.


If I’m wrong then, I request you to describe a little worldly what I need to do.

Thank you

What are you seeing that’s not working for you?

I suspect your Value is not set correctly. The example shows no question mark.

And if I remember (can’t find the reference now), you might not need the equal sign, either, if you’re not trying to match the value of the query string itself.

When I enter domain.com/movie/abc this kind of URL on browser it is showing my custom 404 page. It was supposed to be showing domain.com/movie?q=abc link content.

I did give example on my first post can you please write exactly what I have to do on the transform rule.

Oh, I see. You want the reverse. You want the end of the path to be converted to a Query String.

I don’t think that’s feasible, unless you’re on a Business or Enterprise plan where you can use RegEx.

CORRECTION: It may be possible, but I don’t know the syntax. It involves counting characters, then making a substitution. Maybe another @MVP has the syntax handy.

I want to enter domain.com/movie/abc this type of url on browser, and I want it to show me the content of domain.com/movie?q=abc this page. Any way thank you for your reply. I’m waiting for someone to give me a workable syntax.

There are two things you want:

  1. redirect /*?q=* to domain.tld/$1/$2
  2. serving /$1?q=$2 under domain.tld/*/*

Redirect

The first thing can be solved with a normal Page Rule:


This will redirect all URLs with a query-name equal to “q”.
Example:
Request:
domain.com/movie?q=abc
Redirect:
domain.com/movie/abc

But care, we as work here with wildcards, this also redirects bogus URLs.

Modify

The second thing I personally would solve with workers, as Rules IMHO are not as flexible enough to do very well here.
So for the second part you need to hire a JS developer, who will write you the code for “Cloudflare Workers”
The programmer should:

  1. break the request URL down into its components
    1.1. “path”
    1.2. “query”
  2. then check if your requirements are meet
    2.1. if it passes, modify Request URL and send to origin
    2.2. if not, do not modify and send to origin

Have fun.

4 Likes

Thanks @M4rt1n.

It looks like they want it to go the other way around, and use Rewrite URL. That way, the browser doesn’t show the query string, and not hit a redirect.

The Page Rule syntax is awesome, and I wish that someday they could get these transform rules to work the same way.

2 Likes

This part, the custom Workers should take over. The PageRule just makes sure, that the query URLs are not directly callable and will also result in (redirected to) the “beautiful” URLs by default, even if someone stored the query ones - like crawlers/searchengines/bookmarks etc.

+1

2 Likes

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.