htaccess将文件夹和子文件夹重定向到仅一个URL

时间:2019-02-01 18:49:29

标签: .htaccess redirect

我必须是第一个对htaccess重定向感到沮丧的人。

已搜索但找不到解决方案:

我有几个这样的404:

https://www.example.com/category/uncategorized/page/4/detox
https://www.example.com/category/uncategorized/page/5/detox
https://www.example.com/category/uncategorized/page/6/detox
https://www.example.com/category/uncategorized/page/7/detox

我想这样重定向:

https://www.example.com/category/uncategorized/ 

及其下的所有子文件夹或页面

TO

https://www.example.com/category/alcohol-drug-rehab/

(仅此确切网址,所有子文件夹均应重定向到该子文件夹)

我尝试过

RedirectMatch 301 ^/category/uncategorized/(.*)$ https://www.example.com/category/alcohol-drug-rehab/

...以及其他几种变体。

但是这会导致这样的重定向:

https://www.example.com/category/alcohol-drug-rehab/page/4/detox
https://www.example.com/category/alcohol-drug-rehab/page/5/detox
https://www.example.com/category/alcohol-drug-rehab/page/6/detox
https://www.example.com/category/alcohol-drug-rehab/page/7/detox

如何更改重定向以停止显示子文件夹的重写URL,然后仅重定向到页面https://www.example.com/category/alcohol-drug-rehab/

如果可能的话,宁愿使用直接重写语句,而不是mod重写。

谢谢

1 个答案:

答案 0 :(得分:0)

只需忽略初始路径中的所有内容:

$Arr1 = New-Object System.Collections.ArrayList

此规则将同样在HTTP服务器主机配置或动态配置文件内(“htaccess的”文件)工作。显然,重写模块需要加载到http服务器内部并在http主机中启用。如果使用动态配置文件,则需要注意在主机配置中完全启用了它的解释,并且该解释位于主机的$Arr1 = @() $Arr2 = @() $collection = New-Object System.Collections.ArrayList ForEach($number in 120..365) { $Arr1 += (Get-Date).addDays(-$number).ToString("yyyy_MMdd") $Arr2 += (Get-Date).addDays(-$number).ToString("yyyyMMdd") } # have to add each item from the original arrays into the arraylist # otherwise the arraylist will only have a count of 2, the entirety of $Arr1 and $Arr2 respectively foreach($item in $Arr1) { $collection.Add($item) | Out-Null } foreach($item in $Arr2) { $collection.Add($item) | Out-Null } foreach($date in $collection) { Get-Content -Path C:\Users\name\Desktop\Deletions.txt | Where-Object { $_ -like "*$date*" } | Out-File -filepath C:\Users\name\Desktop\DeleteThese.txt -Append } 文件夹中。

还有一个一般性说明:您应该始终喜欢将此类规则放置在http服务器主机配置中,而不要使用动态配置文件(“ .htaccess”)。这些动态配置文件增加了复杂性,通常是导致意外行为,难以调试的原因,并且确实降低了http服务器的速度。仅当您无法访问真正的http服务器主机配置(阅读:真正便宜的服务提供商)或坚持编写自己的规则的应用程序(这显然是安全的噩梦)时,才提供它们作为最后的选择。

相关问题