How do I enable query string in Cloudflare page rules? - cloudflare

I want to forward this URL
https://demo.example.com/page1?v=105
to
https://www.example.com/page1
It's not working if I set so:
But if I remove the ?v= part, it works:
Is there any way to include the ?v= part in this page rule?

The rule you mentioned works only for that exact URL: https://demo.example.com/page1?v=105. If it has any additional query parameter or even an additional character, eg https://demo.example.com/page1?v=1050, it won't match the rule.
If you need to use complicated URL matches that require specific query strings, you might need to use bulk redirects or a Cloudflare worker.

Related

Mod_Rewrite how to ignore slashes that don't belong in url string

I'm trying to setup canonical links in our forum and I need to come up with a rewrite rule that will ignores slashes that don't belong in the URL.
The proper URL would look like:
http://www.truckingtruth.com/truckers-forum/Topic-20315/Page-1/speak-to-recruiter
For this I'm using the following mod_rewrite rule to pass the 'topic', 'page', and 'subjectString' variables:
^Topic-(.*)/Page-(.*)/(.*)$ index.html?topic=$1&page=$2&subjectString=$3
But sometimes improper links to our site or improper links in a comment will add slashes to the URL that don't belong there and it throws off the rule. Example:
http://www.truckingtruth.com/truckers-forum/Topic-1652/Page-1/www.truckingtruth.com/free_truck_driving_schools/swift/how-to-use-the-qualcomm
When that happens the variables being passed are:
topic = "1652"
page = "1/www.truckingtruth.com/free_truck_driving_schools/swift"
subjectString = "how-to-use-the-qualcomm"
What I want it to do is pass:
topic = "1652"
page = "1"
subjectString = "www.truckingtruth.com/free_truck_driving_schools/swift/how-to-use-the-qualcomm"
How can I create a rewrite rule that will pass everything after "Page-1" as the subjectString even if there are slashes in it?
Since the topic is always integer, for your first capturing group you can use \d which matches any decimal digit (equivalent to [0-9]).
For page, just make sure not to include any slashes, [^/] will take care of that.
The rest should then all go to third capturing group, so the resulting regex will be:
^Topic-(\d*)/Page-([^/]*)/(.*)$

htaccess ignore part of query string

It is possible to write a .htaccess rule to ignore part of the query string but still keep it in the url (without redirection)?
i need a way to ignore query string(s) starts with the utm_ like they are absent (invisible to the backend scripts) but still present in the browser URL because the query string will be captured by the JavaScript analytics scripts (thats why solutions with the redirection unacceptable).
lets say i have an url without query string: https://example.com/bla/hello
or with query string: https://example.com/anything?hello=world
then i need to add a query string part utm_source=123 which could be positioned anywhere among the other query string elements
some pages of the site stops working or starts behaving different when im adding a query string to the url for example https://example.com/?utm_source=123 throws 404
if it is possible, could you help me with the rules ?

Apache regex -301 redirects to eradicate duplicates in url path

We are using a CMS that produces URLs of the format www.domain.com/home/help/contact/contact. Here the first occurrence of contact is the directory and the second occurrence is the HTML page itself. These urls are causing issues in the SEO space.
We have implemented canonical tags but the business wants to make sure they don't come across these duplicates in both the search engines and Google analytics, and have asked us to implement a 301 solution on our web server.
My question is we have got a regex to find these matches but I also need the part of the URL before the match. The regex we have is .*?([\w]+)\/\1+ and this returns contact in /home/help/contact/contact. How can I get the /home/help/ path as well so I can redirect to the right page? Can someone help with this please as I am a beginner when it comes to regex?
Since you're able to get contact using a matching group, enclose everything before that inside a matching group as well:
(.*?)(/[\w]+)\2+
I have put the / inside a matching group too, so that you won't get false positives for
/home/some/app/page
this would be \1 ^ ^ found repetition (character p would be matched)

Completely custom path with YII?

I have various products with their own set paths. Eg:
electronics/mp3-players/sony-hg122
fitness/devices/gymboss
If want to be able to access URLs in this format. For example:
http://www.mysite.com/fitness/devices/gymboss
http://www.mysite.com/electronics/mp3-players/sony-hg122
My strategy was to override the "init" function of the SiteController in order to catch the paths and then direct it to my own implementation of a render function. However, this doesn't allow me to catch the path.
Am I going about it the wrong way? What would be the correct strategy to do this?
** EDIT **
I figure I have to make use of the URL manager. But how do I dynamically add path formats if they are all custom in a database?
Eskimo's setup is a good solid approach for most Yii systems. However, for yours, I would suggest creating a custom UrlRule to query your database:
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-custom-url-rule-classes
Note: the URL rules are parsed on every single Yii request, so be careful in there. If you aren't efficient, you can rapidly slow down your site. By default rules are cached (if you have a cache setup), but I don't know if that applies to dynamic DB rules (I would think not).
In your URL manager (protected/config/main.php), Set urlFormat to path (and toptionally set showScriptName to false (this hides the index.php part of the URL))
'urlManager' => array(
'urlFormat' => 'path',
'showScriptName'=>false,
Next, in your rules, you could setup something like:
catalogue/<category_url:.+>/<product_url:.+> => product/view,
So what this does is route and request with a structure like catalogue/electronics/ipods to the ProductController actionView. You can then access the category_url and product_url portions of the URL like so:
$_GET['category_url'];
$_GET['product_url'];
How this rule works is, any URL which starts with the word catalogue (directly after your domain name) which is followed by another word (category_url), and another word (product_url), will be directed to that controller/action.
You will notice that in my example I am preceding the category and product with the word catalogue. Obviously you could replace this with whatever you prefer or leave it out all together. The reason I have put it in is, consider the following URL:
http://mywebsite.com/site/about
If you left out the 'catalogue' portion of the URL and defined your rule only as:
<category_url:.+>/<product_url:.+> => product/view,
the URL Manager would see the site portion of the URL as the category_url value, and the about portion as the product_url. To prevent this you can either have the catalogue protion of the URL, or define rules for the non catalogue pages (ie; define a rule for site/about)
Rules are interpreted top to bottom, and only the first rule is matched. Obviously you can add as many rules as you need for as many different URL structures as you need.
I hope this gets you on the right path, feel free to comment with any questions or clarifications you need

Apache rule to change / (slash) to _ (underscore)

I've set up an Apache HTTP server with VirtualHosts in front of a proprietary web server in the back. The backend server can only have one (1) level in its ID paths so the following public URLs:
http://public-server/path1/path2/path3?querystring-parameters
should be converted for the backend to:
http://internal-server/path1/path2/page/<path1>_<path2>_<path3>?querystring-parameters
Notice that there can be any number of path1, path2, path3, path4, .... and they should all (no matter if only 1 exists or multiple) be concatenated with an underscore. Also notice that the querystring-parameters CAN contain '?', '/' and '_' so the rule should not alter the querystring in any way.
I've tried searching for solutions to this but can't figure out how to overcome the problem. Any suggestions?
If you can come up some maximum number of possible paths, you can do something to this effect:
# This will work for up to 5 paths
RewriteRule /([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*) http://internal-server%{REQUEST_URI}$1_$2_$3_$4_$5 [L,QSA]
The /?([^/]*) can be added to the end as many times as you need, along with added the corresponding groups (_$6 ..) to the rewritten URL.
Unfortunately, there is not a way have a completely unknown number of paths, while at the same time use them in the rewritten URL. Also, the [QSA] flag will attach your querystring on to the forwarded URL, untouched.
Hope this helps.