如何根据内容自动定义 .htaccess 重定向

发布于 2024-12-20 03:21:27 字数 804 浏览 4 评论 0原文

我正在使用

grep -HEri "Title\:(content)" ./www.livesite.com/ > Livesite.txt

grep -HEri "Title\:(content)" ./www.devsite.com/ > Devsite.txt 

来查找具有可以通过正则表达式指定的匹配或相似内容的路径对。当 ^/example_found_live_path.html 中的内容与 ^/ Different/found_devsite_path 中的内容匹配时,我想向开发站点的 .htaccess 文件添加一行,创建 301 重定向从实时站点的路径到开发站点上找到的路径,如下所示:

redirect 301 ^/example_live_path.html ^/different/devsite_path

期望的结果是,在启动时,所有搜索引擎条目和当前实时站点页面的链接都重定向到具有匹配标题的页面在开发站点中。 我觉得这是 sed、grep 和 xargs 的工作,但不知道如何构建命令。 是不是像:

grep -HEri "Title\:(content)" ./www.livesite.com/ | xargs 'echo %1; grep -HEri %2 ./www.devsite.com' | xargs 'sed "$a\nredirect 301 \^%1 \^%2\n' .htaccess

提前致谢!

I am using

grep -HEri "Title\:(content)" ./www.livesite.com/ > Livesite.txt

and

grep -HEri "Title\:(content)" ./www.devsite.com/ > Devsite.txt 

to find pairs of paths which have matching or similar content I can specify by regex. When the content from ^/example_found_live_path.html matches that in ^/different/found_devsite_path, I want to add a line to the dev site's .htaccess file, creating a 301 redirect from the live site's path to the path found on the dev site, that looks like this:

redirect 301 ^/example_live_path.html ^/different/devsite_path

The desired result is that on launch, all search engine entries and links to pages from the current live site redirect to pages with matching titles in the dev site.
I feel like this is a job for sed, grep, and xargs but don't know how to structure the command.
Is it like:

grep -HEri "Title\:(content)" ./www.livesite.com/ | xargs 'echo %1; grep -HEri %2 ./www.devsite.com' | xargs 'sed "$a\nredirect 301 \^%1 \^%2\n' .htaccess

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您已经通过运行以下命令获得了两个文件列表:

grep -HEri "Title\:(content)" ./www.livesite.com/ > Livesite.txt
grep -HEri "Title\:(content)" ./www.devsite.com/ > Devsite.txt 

并得到: (每个找到的文件可能有多个匹配项)

(Livesite.txt)
./www.livesite.com/foo/bar1.xhtml:...Title:(content1)........1a
./www.livesite.com/foo/bar1.xhtml:......Title:(content3)........1b
./www.livesite.com/goo/car.xhtml:....Title:(content4)........1c
...

(Devsite.txt)
./www.devsite.com/bar_dev.html:......Title:(content2)...2a
./www.devsite.com/far_zoo.html:...Title:(content4).........2b
./www.devsite.com/far_zoo.html:...Title:(content3).........2c
...

连接结果应该是:

1a - null
1b - 2c
1c - 2b
null - 2a

这是在匹配内容上内连接两个列表的脚本:

while IFS=: read livepath line1; do
    matching1="${line1#*Title:(}"
    matching1="${matching1%)*}"

    matched=0
    matched_devpath=
    while IFS=: read devpath line2; do
        matching2="${line2#*Title:(}"
        matching2="${matching2%)*}"
        if [ "$matching1" = "$matching2" ]; then
            matched=1
            matched_devpath="$devpath"
            break
        fi
    done <Devsite.txt

    if [ "$matched" = 1 ]; then
        url1="${livepath#./www.livesite.com}"
        url2="${devpath#./www.devsite.com}"
        echo "301 ^$url1 ^$url2"
    fi
done <Livesite.txt

结果应该是:

redirect 301 ^/foo/bar1.html ^/far_zoo.html
redirect 301 ^/goo/car.html ^/far_zoo.html

嗯..祝你好运!

You already had the two file lists by running:

grep -HEri "Title\:(content)" ./www.livesite.com/ > Livesite.txt
grep -HEri "Title\:(content)" ./www.devsite.com/ > Devsite.txt 

and got: (there maybe multiple matchings for each found file)

(Livesite.txt)
./www.livesite.com/foo/bar1.xhtml:...Title:(content1)........1a
./www.livesite.com/foo/bar1.xhtml:......Title:(content3)........1b
./www.livesite.com/goo/car.xhtml:....Title:(content4)........1c
...

(Devsite.txt)
./www.devsite.com/bar_dev.html:......Title:(content2)...2a
./www.devsite.com/far_zoo.html:...Title:(content4).........2b
./www.devsite.com/far_zoo.html:...Title:(content3).........2c
...

The join result should be:

1a - null
1b - 2c
1c - 2b
null - 2a

Here is the script to inner-join the two lists on the matching content:

while IFS=: read livepath line1; do
    matching1="${line1#*Title:(}"
    matching1="${matching1%)*}"

    matched=0
    matched_devpath=
    while IFS=: read devpath line2; do
        matching2="${line2#*Title:(}"
        matching2="${matching2%)*}"
        if [ "$matching1" = "$matching2" ]; then
            matched=1
            matched_devpath="$devpath"
            break
        fi
    done <Devsite.txt

    if [ "$matched" = 1 ]; then
        url1="${livepath#./www.livesite.com}"
        url2="${devpath#./www.devsite.com}"
        echo "301 ^$url1 ^$url2"
    fi
done <Livesite.txt

and the result should be:

redirect 301 ^/foo/bar1.html ^/far_zoo.html
redirect 301 ^/goo/car.html ^/far_zoo.html

Umm.. good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文