htaccess - rewrite multiple directories to query strings on all urls - mod-rewrite

I am trying to find a catch-all solution to redirect every URL to the root file (index.php) with all the directories passed as query strings.
For example;
mywebsite.co.uk/one => mywebsite.co.uk?p1=one
mywebsite.co.uk/one/two => mywebsite.co.uk?p1=one&p2=two
mywebsite.co.uk/one/two/three => mywebsite.co.uk?p1=one&p2=two&p3=three
I only seem to be able to put together rules that work if I know the number of directories/query strings.
Any help would be much appreciated!

Related

System.IO - Use Directory/LINQ to Replace Some Files in a List with Others

I get a list of files using the System.IO Directory thusly:
var srcFiles = Directory.GetFiles(remotePath);
I also have a comma separated list of strings, each of which I want to check for NON-existence in the names of the above files. For example:
string[] filterOn = EndPoint.FileNameDoesNotContainFilter.Split(',').ToArray();
gives me the following array:
filterOn contains ["GoodFile", "EvenBetterFile"]
Now, the files without either of the two strings would replace all the files currently in the srcFiles list above (or a new list, if that makes more sense). I am trying to do this with LINQ, but can't quite get there. How is it done?
EDIT: The answer from #dvo gives me the correct files, however, sometimes the filter strings are contained in the remotePath passed in.
A typical path/file: C:\TEMP\APPS\AMS\Services\sc0189v\APPS\GoodFile\test.txt.
As you can see, "GoodFile" is in the path, but not the filename. Yet this file should be rejected. I suppose I'm looking for something in System.IO.Directory that might help. Not sure, really.
Try this:
var noMatch = srcFiles.Where(file => !filterOn.Any(filter => file.ToUpperInvariant().Contains(filter.ToUpperInvariant()))).ToList();
This will create a new list where the file name does not contain any filter. This ignores case by converting both the file name and filter to uppercase. You can store the results in the same list if you replace noMatch with srcFiles. You can make it case sensitive if you remove .ToUpperInvariant() from both parts of the Any clause. You can capture the filter matches by removing the ! from the Where clause.
Hope this helps!

url rewrite pattern generates to many redirects

I have been struggling with the same issue for a while now and I could not find an good answer yet. I'm using rack-rewrite to add some url rewrite rules to my app's middleware stack.
I have the following rule:
r301 %r{^/([^(docs|help|legal|login|account|apps)])(.+)/$}, '$1'
Which is not working properly or as I would expect it. I have tried one of my previous question's answer, but neither that works, it actually generates an event more weird behaviour (it redirects to an url without the domain name, just to the path).
What I am trying to do is:
if user requests http://example.com/ or http://example.com/random-path/ I need the rewrite rule to strip the slash, thus the examples would become http://example.com respectively http://example.com/random-path;
if the requested paths matches any of the paths in the list docs|help|legal|login|account|apps, do not strip the slash at the end of the path if exists, but add a slash if it's not there
I tried with two rules, one that ignores the listed paths above and strips slashes and one that adds the slash if it hits something from the list and the slash after the path is not there:
r301 %r{^/([^(docs|help|legal|login|account|apps)])(.+)/$}, '/$1'
r301 %r{^/([(docs|help|legal|login|account|apps)])(.+)/$}, '/$1/'
How could I write a rule that would do that, or two rules, because what I tried it did not work?
You can do that like so:
r301 %r{^/((?!docs|help|legal|login|account|apps).+)/$}, '/$1'
r301 %r{^/((?=docs|help|legal|login|account|apps).+[^/])$}, '/$1/'
example 1
example 2
and some documentation on lookahead and lookbehind
EDIT: stray parentheses.

ruby regex for removing url prefix and ending

I've been trying to figure this out, and i've searched but I'm stuck.
Lets say I have the string www.google.com or http://google.com or just google.com
and I want to extract the string google out of those parameters.
A solution I can think of is first removing the first parameters (www.) then removing the second section of the string (.com) but I know there is a similar more efficient way.
any help would be greatly appreciated!
First, start with a tool designed to work with URLs. Ruby includes URI, and there's also Addressable::URI.
Using these you can strip down a URI into its defined components:
require 'uri'
uri = URI.parse('http://www.ruby-doc.org/stdlib-2.1.1/libdoc/uri/rdoc/URI.html')
uri.host # => "www.ruby-doc.org"
If your string doesn't start with a scheme, you can add one. (Schemes are important.)
url = 'foo.bar.com/some/path'
URI.parse('http://' + url).host
# => "foo.bar.com"
From that point you're going to have a tough time determining what is the true host, versus the domain. A domain can be anything (pretty much) and the host can be the domain name. Possibly you can get a list of domains but, remember that the list is constantly changing.
ICANN has a list of TLDs, as does IANA. Those are ONLY the top-level-domains, not the hosts that sit under them. However, using those lists you can strip the TLD from a host, and at least be a tiny bit closer to where you want to be.

How to get the last word from a URL?

Is there a method to extract just the last word from the URL example below? I would like to be able to use this as a heading on a page, i.e the "Account" page.
I found that by using request.path it will give me the path without the root but I'm not sure how to get just the last path name.
/users/1234/account
Try:
request.path.split('/').last
If you want "Account" (instead of "account"), call the capitalize method on the result.
I am not familiar with Ruby, but you can try this approach.
Try string splitting request.path with '/' as the separator and take the last element from the resulting array
users/1234/account will be split to {'user', '1234', 'account'}
Even though this doesn't answer your question directly, I hope it gives you a start
URLs are a simple string consisting of a scheme showing how to connect to a site, the host where the resource is located, plus a path to that resource. You can use File.basename to get the last part of that path, just like we'd use on a file on our disk:
File.basename('/users/1234/account')
=> "account"
Suppose you have URL like https://www.google.com/user/lastword
If you want to store last word of the URL which is lastword in a variable then use the following and pass url as value to finalVal.
var getLastWordFromUrl = finalVal.split("/").last()

URL rewriting with ISAPI Rewrite

I am trying to create a filter system for products and keep the URL's friendly, i am using a map file for the filter params and the following re-write code.
The url could contain, one param or 10 params and will be bulit like so
Start URL: www.domain.com/climbing-frames/
First Param: www.domain.com/climbing-frames_rockwall/
Second Param: www.domain.com/climbing frames_rockwall_rope-ladder/
And so on.....
The rewrite rule so far
RewriteMap features txt:features.txt
RewriteCond ${features:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^climbing-frames(?:_([^/]+))(?:_([^/]+))(?:_([^/]+))(?:_([^/]+))/ /get-features.cfm?(?1(param1=${features:$1|0}))(?2(,${features:$2|0}))(?3(,${features:$3|0}))(?4(,${features:$4|0}))
This works if 4 prams are entered but not if only two params are entered. This is driving me crazy!
Any clues would be greatly appreciated.
Jason
I think you're close - there might be a looping approach that would be better, but I do tend to go with the way you're going.
I see how you're doing the non-capturing underscore, then capturing the next characters. Instead of looking for not-slash, look for not-underscore. With the not-slash, it's going to grab the whole thing in the first group. That is, look for underscore-not-underscore.
Then (getting to the real fix), each section needs to be optional - the rule above is requiring the four sections. So add a ? to the end of each one, as:
RewriteRule ^climbing-frames(?:_([^_]+))(?:_([^_]+))?(?:_([^_]+))?(?:_([^_]+))?/
I left the first group required, since it practically should be there, and the RewriteCond is checking for it anyway.

Resources