.htaccess 在RewriteRule match中使用环境变量-更易于维护且更小
我要为一个非常旧的站点做大量的重写,这个站点有许多不同的目录结构。大约需要140个重定向,以下是结构示例:.htaccess 在RewriteRule match中使用环境变量-更易于维护且更小,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我要为一个非常旧的站点做大量的重写,这个站点有许多不同的目录结构。大约需要140个重定向,以下是结构示例: # First file set to first destination RewriteRule ^(dir-one|dir-two|dir-three)\/(file-one|file-two|file-three)\/?$ /destination-one [R=301,L] # Second file set to second destination RewriteRule ^
# First file set to first destination
RewriteRule ^(dir-one|dir-two|dir-three)\/(file-one|file-two|file-three)\/?$ /destination-one [R=301,L]
# Second file set to second destination
RewriteRule ^(dir-one|dir-two|dir-three)\/(file-four|file-five|file-six)\/?$ /destination-two [R=301,L]
# Third file set to third destination
RewriteRule ^(dir-one|dir-two|dir-three)\/(file-seven|file-eight|file-nine)\/?$ /destination-three [R=301,L]
# etc etc... Same sort of thing another 137 times!
正如您所看到的,在初始目录匹配中有很多重复信息(dirone | dirtwo | dirthree
)
我想-如果可能的话,使目录列表易于更新,并想知道环境变量是否工作。以下是我尝试过的:
# setting the dir names in the ENV:
RewriteRule .* - [E=DIRS:"dir-one|dir-two|dir-three"]
# First file set to first destination
RewriteRule ^(%{ENV:DIRS})\/(file-one|file-two|file-three)\/?$ /destination-one [R=301,L]
# Second file set to second destination
RewriteRule ^(%{ENV:DIRS})\/(file-four|file-five|file-six)\/?$ /destination-two [R=301,L]
# Third file set to third destination
RewriteRule ^(%{ENV:DIRS})\/(file-seven|file-eight|file-nine)\/?$ /destination-three [R=301,L]
这是行不通的。我知道ENV正在设置中(见下面的屏幕截图),但它们似乎没有在重写规则中使用。我做错了什么,这可能吗,还有更好的办法吗
如果这是可行的,我可能会扩展它,将文件集保存为环境,这样文件集也可以在一个地方更新
谢谢
一些澄清。。。
我的例子没有很好地说明我正在努力实现的目标。这里有一个更“真实”的例子:
# What I have now. Note the repeated 'sections|categories|areas' part:
RewriteRule ^(sections|categories|areas)\/(car|plane|train)\/?$ /transport [R=301,L]
RewriteRule ^(sections|categories|areas)\/(pig|cow|goat|kangaroo)\/?$ /animals [R=301,L]
RewriteRule ^(sections|categories|areas)\/(cheese|fish|turnips)\/?$ /food [R=301,L]
# etc etc... Same sort of thing another 137 times!
因此,每次重写,第一个匹配总是相同的。第二个匹配和目标之间存在不同的映射。i、 e所有车辆进入/transport
,所有动物进入/animals
等
我的主要目标是避免第一场比赛重演140次。如果我的目标是愚蠢的,请说:)
谢谢大家! 在
RewriteCond
中匹配ENV
而不是在RewriteRule
中,如下所示:
RewriteRule ^(dir-one|dir-two|dir-three)/(.*)$ - [E=DIRS:$1]
RewriteCond %{ENV:DIRS} ^dir-one
RewriteRule ^([^\/]+)\/(file-one|file-two|file-three)\/?$ /destination-one [R=301,L]
RewriteCond %{ENV:DIRS} ^dir-two
RewriteRule ^([^\/]+)\/(file-four|file-five|file-six)\/?$ /destination-two [R=301,L]
RewriteCond %{ENV:DIRS} ^dir-three
RewriteRule ^([^\/]+)\/(file-seven|file-eight|file-nine)\/?$ /destination-three [R=301,L]
RewriteRule ^(dir-one|dir-two|dir-three)/(.*)/?$ - [E=DIRS:$1-$2]
RewriteCond %{ENV:DIRS} ^dir-one\-(file-one|file-two|file-three)
RewriteRule ^ /destination-one [R=301,L]
RewriteCond %{ENV:DIRS} ^dir-two\-(file-four|file-five|file-six)
RewriteRule ^ /destination-two [R=301,L]
RewriteCond %{ENV:DIRS} ^dir-three\-(file-seven|file-eight|file-nine)
RewriteRule ^ /destination-three [R=301,L]
此外,如果您只想将它们重定向到新的区分,并且新旧uri之间没有关系,如我所见,请将它们设置为:
RewriteRule ^(dir-one|dir-two|dir-three)/(.*)$ - [E=DIRS:$1]
RewriteCond %{ENV:DIRS} ^dir-one
RewriteRule ^([^\/]+)\/(file-one|file-two|file-three)\/?$ /destination-one [R=301,L]
RewriteCond %{ENV:DIRS} ^dir-two
RewriteRule ^([^\/]+)\/(file-four|file-five|file-six)\/?$ /destination-two [R=301,L]
RewriteCond %{ENV:DIRS} ^dir-three
RewriteRule ^([^\/]+)\/(file-seven|file-eight|file-nine)\/?$ /destination-three [R=301,L]
RewriteRule ^(dir-one|dir-two|dir-three)/(.*)/?$ - [E=DIRS:$1-$2]
RewriteCond %{ENV:DIRS} ^dir-one\-(file-one|file-two|file-three)
RewriteRule ^ /destination-one [R=301,L]
RewriteCond %{ENV:DIRS} ^dir-two\-(file-four|file-five|file-six)
RewriteRule ^ /destination-two [R=301,L]
RewriteCond %{ENV:DIRS} ^dir-three\-(file-seven|file-eight|file-nine)
RewriteRule ^ /destination-three [R=301,L]
更新:
根据您的更新,请尝试以下操作:
RewriteEngine on
RewriteRule ^(sections|categories|areas)/(.*)/?$ - [E=DIRS:$1-$2]
RewriteCond %{ENV:DIRS} ^(.+)\-(car|plane|train)
RewriteRule ^ /transport [R=301,L]
RewriteCond %{ENV:DIRS} ^(.+)\-(pig|cow|goat|kangaroo)
RewriteRule ^ /animals [R=301,L]
RewriteCond %{ENV:DIRS} ^(.+)\-(cheese|fish|turnips)
RewriteRule ^ /food [R=301,L]
注意:清除浏览器缓存,然后测试在重写条件下与ENV的匹配不重写规则感谢您回答Mohammed。目录和目标之间没有关系。文件名和目标之间存在关系。我可能需要改进我的示例?@MereDevelopment这两条规则都适用于您指定的内容,但如果还有其他内容,请告诉我路径是否为:
/dir one/file eight
我希望它重定向到/destination third
。同样,如果/dir-two/文件8
应该重定向到/destination-two
。这不合理吗?@MereDevelopment好的,让我换个颜色rules@MereDevelopment三号目录/一号文件怎么样?是目的地一号吗?