重写URL正则表达式:不同的文件名到相同的URL结构

时间:2017-01-20 16:42:23

标签: regex apache .htaccess mod-rewrite

e.g。从

setDT(df)[, .(sum = sum(cbind(L2, L3, L4) > 0, na.rm = TRUE)), by = Name]

##    Name sum
## 1: Carl   4
## 2:  Joe   4

很容易获得:

A: www.example.com/food.php
B: www.example.com/food2.php?type=bacon
C: www.example.com/food2.php?type=tomato

但是如何从原始网址结构中获取以下内容?

A: www.example.com/food
B: www.example.com/food2/bacon
C: www.example.com/food2/tomato

我是否需要保留相同的文件名才能获得所需的网址,或者是否有一些正则表达式可以在不造成问题的情况下执行此操作?

我对正则表达式没有多少经验,只是从模板中进行简单的重写。

3 个答案:

答案 0 :(得分:1)

使用此:

RewriteEngine On
RewriteRule ^food/([^/]*)$ /food2.php?type=$1 [L]

这将为您留下URL:

www.example.com/food/bacon

www.example.com/food/tomato

确保在测试之前清除缓存。

答案 1 :(得分:1)

重写配置应如下所示:

Sub getData() 
    Dim wbRng As Range, cell As Range, f As Range
    Dim strStatus, strStatusNum, strD1 

    Application.EnableEvents = False
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.Calculation = xlManual


    With ActiveWorkbook.ActiveSheet
        Set wbRng = .Range("A7:A" & WorksheetFunction.Max(7, .Cells(.Rows.count, 1).End(xlUp).Row)) '<--| set the range of values to be searched for
        If WorksheetFunction.CountA(wbRng) = 0 Then Exit Sub '<--| exit if no values under row 7
        Set wbRng = wbRng.SpecialCells(xlCellTypeConstants)  '<--| narrow the range of values to be searched for down to not blank values only
    End With

    With Workbooks.Open("D:\Files\test2.xlsx", True, True).Sheets(1) '<--| open wanted workbook and reference its first sheet
        With .Range("A1:A" & .Cells(.Rows.count, "H").End(xlUp).Row) '<--| reference its column A range from row 1 down to column H last not empty cell (this is your former "srcRange")
            For Each cell In wbRng.SpecialCells(xlCellTypeConstants) '<--| loop through range of values to be searched for
                Set f = .Find(what:=cell.Value, lookat:=xlWhole, LookIn:=xlValues) '<--| look referenced range for current value to be searched for
                If Not f Is Nothing Then '<--| if found
                    strStatus = f.Offset(, 2).Value
                    strD1 = f.Offset(, 3).Value

                    'Convert strStatus to actual number e.g. "03. no d1"
                    strStatusNum = val(Left(strStatus, 2)) '<--| use 'Val()' function to convert string "03" to "3"
                    cell.Offset(, 3) = strStatusNum
                    Select Case True
                        Case strStatusNum <> 3
                            cell.Offset(, 1).Value = "Not at 03. No Work Order"
                        Case strStatusNum = 3 And (strD1 <> "")
                            cell.Offset(, 1).Resize(, 2).Value = Array("D1 Received", strD1)
                        Case Else
                            cell.Offset(, 1).Value = "No D1"
                    End Select
                End If
            Next
        End With
        .Parent.Close False
    End With

    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    Application.Calculation = xlAutomatic

End Sub

有关详细信息,请参阅mod_rewrite documentation

请不要与&#34; $ 12&#34;混淆反向引用 - mod_rewrite仅提供从1美元到9美元的一位数引用(对于整个匹配的字符串为0美元),因此它引用第一个(组),后跟&#34; 2&#34;

答案 2 :(得分:0)

你想要这个吗?我使用了vim的替换命令。

enter image description here

enter image description here