php - 如何为隐藏链接创建永久重定向脚本
问题描述
我目前正在使用Yoast 隐藏脚本为附属链接创建临时重定向 (302)。例如,如果我导航到https://website.com/go/Site2,它会重定向到“https://redirectwebsite2.com”。
如何为永久重定向 (301) 调整以下内容?我尝试将标题从 302 更改为 301,但这不起作用。
以下文件保存在“public_html”WordPress 目录的“go”文件夹中。
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]
</IfModule>
索引.php
<?php
$id = isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : 'default';
$f = fopen( 'redirects.txt', 'r' );
$urls = array();
// The file didn't open correctly.
if ( !$f ) {
echo 'Make sure you create your redirects.txt file and that it\'s readable by the redirect script.';
die;
}
// Read the input file and parse it into an array
while( $data = fgetcsv( $f ) ) {
if ( !isset( $data[0] ) || !isset( $data[1] ) )
continue;
$key = trim( $data[0] );
$val = trim( $data[1] );
$urls[ $key ] = $val;
}
// Check if the given ID is set, if it is, set the URL to that, if not, default
$url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : false );
if ( $url ) {
header( "X-Robots-Tag: noindex, nofollow", true );
header( "Location: " . $url, 302 );
die;
} else {
echo '<p>Make sure yor redirects.txt file contains a default value, syntax:</p>
<pre>default,http://example.com</pre>
<p>Where you should replace example.com with your domain.</p>';
}
重定向.txt
default,https://website.com
Site1,https://redirectwebsite1.com
Site2,https://redirectwebsite2.com
解决方案
解决方案是将标头更改为 ( "X-Robots-Tag: noindex, nofollow", true, 301 )
<?php
$id = isset( $_GET['id'] ) ? rtrim( trim( $_GET['id'] ), '/' ) : 'default';
$f = fopen( 'redirects.txt', 'r' );
$urls = array();
// The file didn't open correctly.
if ( !$f ) {
echo 'Make sure you create your redirects.txt file and that it\'s readable by the redirect script.';
die;
}
// Read the input file and parse it into an array
while( $data = fgetcsv( $f ) ) {
if ( !isset( $data[0] ) || !isset( $data[1] ) )
continue;
$key = trim( $data[0] );
$val = trim( $data[1] );
$urls[ $key ] = $val;
}
// Check if the given ID is set, if it is, set the URL to that, if not, default
$url = ( isset( $urls[ $id ] ) ) ? $urls[ $id ] : ( isset( $urls[ 'default' ] ) ? $urls[ 'default' ] : false );
if ( $url ) {
header( "X-Robots-Tag: noindex, nofollow", true, 301 );
header( "Location: " . $url );
die;
} else {
echo '<p>Make sure yor redirects.txt file contains a default value, syntax:</p>
<pre>default,http://example.com</pre>
<p>Where you should replace example.com with your domain.</p>';
}
推荐阅读
- memory - Visual Studio 2019 内存使用率高
- c# - 在 WCF 服务(SVC 文件)中使用 C# 的类似 WSDL 结构
- python - 创建具有多个堆栈的 azure Web 应用程序
- c++ - 结构化绑定违规
- c++ - 什么是 c++ 初始化中函数式转换的模棱两可的转换
- reactjs - React Js 时间选择器
- javascript - 在 ReactJs 中播放 ulaw 音频
- html - 用省略号截断文本但不截断最后一个单词
- prometheus - 具有动态标签值的 Prometheus 指标
- python - 如何在 tensorflow keras 模型中使用从拥抱面变压器生成 gpt2?