Php 如何为隐藏链接创建永久重定向脚本
我目前正在使用为附属链接创建临时重定向(302)。例如,如果我导航到它,将重定向到“https://redirectwebsite2.com“ 如何调整以下永久重定向(301)?我试着将标题从302改为301,但是没有用 以下文件保存在“public_html”WordPress目录下的“go”文件夹中 .htaccessPhp 如何为隐藏链接创建永久重定向脚本,php,wordpress,.htaccess,redirect,Php,Wordpress,.htaccess,Redirect,我目前正在使用为附属链接创建临时重定向(302)。例如,如果我导航到它,将重定向到“https://redirectwebsite2.com“ 如何调整以下永久重定向(301)?我试着将标题从302改为301,但是没有用 以下文件保存在“public_html”WordPress目录下的“go”文件夹中 .htaccess <IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^index\.php$ - [L] RewriteR
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]
</IfModule>
解决方案是将标题更改为(“X-Robots-Tag:noindex,nofollow”,true,301)
当您从302更改为301时,发生了什么?您期望结果是什么?我正在尝试创建一个301(永久)重定向。链接的标题仍然显示302,而不是预期的301。该函数将布尔值作为第二个参数,尝试将状态代码移动到第三个参数。我想我找到了解决方案。我需要将标题更改为(“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 );
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>';
}
default,https://website.com
Site1,https://redirectwebsite1.com
Site2,https://redirectwebsite2.com
<?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>';
}