从带有查询字符串的url重写到文件夹

时间:2018-05-08 02:38:41

标签: regex url-rewriting .net-core

我想要像这样重写网址:

/files/b9f8d0b5e35248579953755b3677a59b.png?w=400&h=100&mode=crop

要:

/files/400/100/crop/b9f8d0b5e35248579953755b3677a59b.png

我的规则如下:

.AddRewrite(@"^files/(.*)?w=(\d+)&h=(\d+)&mode=(.*)$", "files/$2/$3/$4/$1", true)

但它不起作用,我该如何解决?非常感谢!

1 个答案:

答案 0 :(得分:1)

您的正则表达式以files/开头,这使得该模式仅在以[^?]开头时才匹配。否则看起来不错。我使用?作为匹配除[^&]之外的任何内容的字符组,以及AddRewrite(@"/files/([^?]+)\?w=(\d+)&h=(\d+)&mode=([^&]+)", "/files/$2/$3/$4/$1", true) 的类似字符组。

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
    string inputFileName;
    string name, gender, college;
    double score = 0, totalCC = 0, totalUN = 0, totalFemalesScore = 0, totalMalesScore = 0;
    int ccCount = 0, unCount = 0, fCount = 0, mCount = 0;
    ifstream inputFile;

    cout << "Input file name: ";
    getline(cin, inputFileName);

    inputFile.open(inputFileName);

    if (!inputFile.is_open())
    {
        cout << "Unable to open input file." << endl;
        exit(1);
    }
    while (inputFile.peek() != EOF)
    {
        inputFile >> name >> gender >> college >> score;
        cout << left << setw(17) << name << setw(4) << gender << setw(4) << college << setw(4) << score << endl;
    }
    cout << "\nEnd of file reached\n" << endl;
    inputFile.clear();
    inputFile.seekg(0);

    while (inputFile >> name)
    {
        inputFile >> gender >> college >> score;
        if (gender == "F")
        {
            totalFemalesScore += score;
            fCount++;
        }
    }
    while (inputFile >> name)
    {
        inputFile >> gender >> college >> score;
        if (gender == "M")
        {
            totalMalesScore += score;
            mCount++;
        }
    }
    while (inputFile >> name)
    {
        inputFile >> gender >> college >> score;
        if (college == "CC")
        {
            totalCC += score;
            ccCount++;
        }
    }
    while (inputFile >> name)
    {
        inputFile >> gender >> college >> score;
        if (college == "UN")
        {
            totalUN += score;
            unCount++;
        }
    }
    cout << "\nEnd of file reached\n" << endl;
    cout << fixed << showpoint << setprecision(2);
    cout << "Average for females = " << totalFemalesScore / fCount << endl;
    cout << "Average for males = " << totalMalesScore / mCount << endl;
    cout << "Average of CC students = " << totalCC / ccCount << endl;
    cout << "Average of UN students = " << totalUN / unCount << endl;

    inputFile.close();
    system("pause");
    return 0;
}

^在https://www.regexplanet.com/share/index.html?share=yyyyye98k3r

上进行了测试

您可能想要考虑如果查询参数顺序发生变化会发生什么。