Rewrite path segments with name value pairs to query parameters - regex

I want to rewrite pretty urls to a query parameter in my .htaccess file using regex.
So that:
https://www.example.com/s/file/name1/value1/name2/value2/name3/value3
gets rewritten to:
https://www.example.com?file.htm?name1=value1&name2=value2&name3=value3
It needs to handle variable numbers of name pair values. It will include at least one name value pair. Eg it needs to work with https://www.example.com/s/file/name1/value1 and https://www.example.com/s/file/name1/value1/name2/value2
The s in https://www.example.com/s/file/name1/value1/name2/value2/name3/value3 indicates the rewrite rule should trigger: all other urls should be left alone. The file value is the name of the htm file, so this can have different values.
In regex101.com I have tried:
pattern: \/([^\/]+)(\/)([^\/]+)
substitution: $1=$3&
On string: /s/new/v/123/c/42
And it returns: s=new&v=123&c=42&
But it should return: new.htm?v=123&c=42
So I have successfully gotten it to work with a variable number of name value pairs. But I just can't get my head around how to make it first move past s and new and then dynamically replace name value pairs.
I did not include https://www.example.com/ in regex101 because in a .htaccess file the initial domain is assumed.
I found this method but it seems to work with a fixed amount of value pairs.
I also reviewed this post which contains great information, but no solution to this specific issue.

In the end I simplified what I needed. In the htaccess file I put:
RewriteRule ^v/([^/]+)/([^/]+) /voucher/$1.htm?v=$2 [NC,R,L]
So now a pretty url that looks like
https://www.example.com/v/page/id
gets rewritten to
https://www.example.com/voucher/page.htm?v=id

Related

Insert link to local file onto xwiki

Hi this may apply to platforms/wikis outside of xwiki, but I am trying the embed a file by doing the following
[[myfile>>file://C:/users/myfile.txt]]
where clicking on the newly created link does nothing.
I have tried with backslashed file path too but no difference and three slashes infront of "file:"
this should be pretty straightforward....
There should be three slashes in a URI like file:///C:/.
After the "protocol" part, the file URI scheme takes first a host name (which can be omitted in your case, because you are trying to access a local resource), then the path. Between host and path there is a slash. (This holds for other URI schemes, as well...)
The slash has to remain, even if the host part is omitted.

Find domain and remove other parts from URL

I have a list of domain name with parameters
www.frontdir.com/index.php?adds1205
centurydirectory.com/submit/
www.directoryhigher.com/index.php?filec-linkapproval&x_response_code1
I need to find other parts with domain and I have to replace those parts.
Finally my result should look as follows.
Expected result:
www.frontdir.com
centurydirectory.com
www.directoryhigher.com
I tried the following regex
/([^/\?]+)\?
but can not able select after " ? "
How can I attain this result?
How about replacing
\/.*$
with an empty string?
I'm assuming here that you have one URL per line (your example suggests as much) and that you want to keep just the domains (again, as per your example).

How to guess full file name, having only first 2 letters

I have a directory full of files, which names are prefixed with sequential, unique number - like so:
/01 - Gruppe #1 - Potatisvalsen.mp3
/02 - Gruppe #1 - Wondrous Love & Hell Broke Loose in Georgia.mp3
Those are accessible at http://mysite/01 - Gruppe #1 - Potatisvalsen.mp3 etc.
I would like to rewrite calls like http://mysite/01.mp3 to the correct full URL as above.
I have tried the "obvious":
RewriteRule ^/(\d+)*\.mp3$ ./$1(.*)\.mp3
But that probably just shows my ignorance :)
Is this possible using mod_rewrite?
mod_rewrite cannot do this shell expansion. You will be better off forwarding these requests to a PHP script and load the actual file there.
Step 1: Forward to PHP
RewriteRule ^\d{2}\.mp3$ fileloader.php?f=$0 [L,QSA,NC]
Step 2: Inside fileloader.php
Load a list of files from current directory into an associative array
Perform a lookup on those filename using $_GET['f']
Serve the found file

RegEx to rewrite folder structure of varying length and file name to string

This is the code I'm using, developed with the help of #anubhava to rewrite a path generated by a CGI script to redirect the path from the location of my jpg image files to another folder that contains watermarked image files in the same folder structure organization as the originals, but exclude files that begin with tn_ or AM (plus _category_image.jpg):
RewriteRule ^ImageFolio4_files/1/([^/]+)/((?!AM|tn_)[^.]+\.jpg)$ /ImageFolio4_files/cache/images/~$1~$2 [L,R=302,NC]
The original path of:
/ImageFolio4_files/1/Casual_Portraits/abc123_789-xyz.jpg
And the above RegEx works to properly generate this output:
/ImageFolio4_files/cache/images/~Casual_Portraits~abc123_789-xyz.jpg
My CHALLENGE: I need to accommodate a multi-folder structure up to three folders deep underneath the ImageFolio4_files/1/ structure. The current code doesn't accomodate that. I also need to exclude any files named _category_image.jpg which occurs at each of the folder levels beneath ImageFolio4_files/1/ (these files are unique small display icons that appear next to the category names)
I really have no idea how to accomodate the multi-folder structure so your help would be appreciated.
First, change
([^/]+)/ to (([^/]+)/)+
in your expression.
Second, change
(?!AM|tn_) to (?!AM|tn_|_category_image.jpg)
You can use the the negative lookahead (?!) for the whole filename as well, it doesn't eat up characters, just checks if the regex "AM|tn_|_category_image.jpg" matches at the actual position.

What is the mappings.ts file and how should it be set up in Tritium?

I'm using the Moovweb SDK and using Tritium. I want my mobile site to behave like my desktop site. I have different URLs pointing to my homepage. Should I use regex? A common element? And what's the best syntax for matching the path?
The mappings.ts file in the scripts directory is where particular pages are matched. The file is imported in html.ts and allows us to say "when a certain page is matched, make the following transformations."
Most projects already have a mappings file generated. A simple layout will be as so:
match($path) {
with(/home/) {
log("--> Importing pages/homes.ts in mappings.ts")
#import pages/home.ts
}
}
Every time you start working on a new page, you need to set up a new "map".
First: Match with a unique path
The Tritium above matches the path for the homepage. The path is the bit of a URL after the domain. For example, in www.example.com/search/item, "www.example.com" is the domain and "search/item" is the path.
The <>/home/<> is specifying the "home" part with regular expressions. You could also use a plain string if necessary:
with("home")
If Tritium matches the path with the matcher, it will import the home page.
It's probably true that the homepage of a site doesn't actually contain the word home. Most homepages are the URL without any matcher. A better string matcher could be:
match($path) {
with ("/")
}
Or, using regex:
with(/index|^\/$/) {
As you can see, the <>with()<> function of the mappings file is where knowledge of Regex can really come in handy. Check out our short guide on regex. Sometimes it will be simpler, such as <>(/search/)<>.
Remember to come up with the most unique aspect of the URL possible. If two <>with()<> functions match the same URL, then the one that appears first in the mappings file will be used. If you cannot find a unique URL matcher for different page types, you may have to match via other means.
Why Use Regex?
It might seem easier to use a string rather than a regex matcher. However, regex provides a lot more flexibility over which URLs are matched.
For example, a site could use a string of numbers in its product page URLs. Using a normal string matcher would not be practical - you'd have to list out all the numbers possible for all the items on the site. An easier way would be to use regex to say, "If there's a string of 5 digits, continue!" (The code for matching 5 digits: <>/\d{5}/<>.)
Second: Log the match
When matching a particular path, you should also use <>log()<> statements so you know exactly what's getting imported. The log statement will be printed in the command line window, so you can see if your regular expression accurately matches your path.
match($path) {
with(/index|^\/$/) {
log("--> importing pages/home.ts in mappings.ts")
}
}
Third: Import the file
Finally, use the <>#import<> function to include the page-specific tritium file.
match($path) {
with(/index|^\/$/) {
log("--> importing pages/home.ts in mappings.ts")
#import pages/home.ts
}
}