INSERT_WITH_MARKETS()WordPress和HTAccess帮助

时间:2015-08-27 作者:Mark

我正在尝试开发一个插件,在中添加一行代码。WordPress生成的htaccess文件。

问题是,WP生成的一行覆盖了我的规则,并且通过“insert\\u with\\u markers()”函数,我还没有找到一种方法来指定位置,以便我可以预先添加行,而不是像当前那样添加行。

第一行代码是自动生成的WordPress行,第二行代码是我希望使用的代码。

RewriteRule . /dev/index.php [L]

RewriteRule ^article/([/_0-9a-zA-Z-]+)$ http://www.domain.tld/?id=$1 [R=301,L,NC]
在我看来,这里有两种可能性:

A) 以某种方式指定insert\\u with\\u markers()将新行代码放置在何处,或

B) 修改我的重写规则以与WordPress生成的规则协同工作。

我希望你能为我指出一到两步更接近正确的道路。

谢谢

3 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

我建议另一种方法:重写API-你知道它支持外部URL吗*?

function wpse_199898_add_htaccess_rule() {
    // No need for the caret "starts with", WP will add it
    add_rewrite_rule( \'article/([/_0-9a-zA-Z-]+)$\', \'http://www.domain.tld/?id=$1 [R=301,L,NC]\' );
}

add_action( \'init\', \'wpse_199898_add_htaccess_rule\' );
*差不多。我们只需要修复WP奇怪的路径前缀:

function wpse_199898_fix_htaccess_rule( $rules ) {
    return str_replace( \'/http://www.domain.tld/\', \'http://www.domain.tld/\', $rules );
}

add_filter( \'mod_rewrite_rules\', \'wpse_199898_fix_htaccess_rule\' );

SO网友:Mark

在阅读了这个问题之后,我决定完全采用另一种方法,并挂接到过滤器中,而不是使用insert\\u with\\u markers()

SO网友:Den Pat

如果其他人想要的功能。htaccess代码插入,从核心WordPress文件复制WordPress功能:wp admin/includes/misc。php:

第109行:函数insert\\u with\\u markers($filename,$marker,$insertion){

以下功能仅供参考:

if ( ! function_exists( \'insert_with_markers\' ) ) {
    function insert_with_markers( $filename, $marker, $insertion ) {
    if ( ! file_exists( $filename ) ) {
        if ( ! is_writable( dirname( $filename ) ) ) {
            return false;
        }

        if ( ! touch( $filename ) ) {
            return false;
        }

        // Make sure the file is created with a minimum set of permissions.
        $perms = fileperms( $filename );
        if ( $perms ) {
            chmod( $filename, $perms | 0644 );
        }
    } elseif ( ! is_writeable( $filename ) ) {
        return false;
    }

    if ( ! is_array( $insertion ) ) {
        $insertion = explode( "\\n", $insertion );
    }

    $switched_locale = switch_to_locale( get_locale() );

    $instructions = sprintf(
        /* translators: 1: Marker. */
        __(
            \'The directives (lines) between `BEGIN %1$s` and `END %1$s` are
dynamically generated, and should only be modified via WordPress filters.
Any changes to the directives between these markers will be overwritten.\'
        ),
        $marker
    );

    $instructions = explode( "\\n", $instructions );
    foreach ( $instructions as $line => $text ) {
        $instructions[ $line ] = \'# \' . $text;
    }

    /**
     * Filters the inline instructions inserted before the dynamically generated content.
     *
     * @since 5.3.0
     *
     * @param string[] $instructions Array of lines with inline instructions.
     * @param string   $marker       The marker being inserted.
     */
    $instructions = apply_filters( \'insert_with_markers_inline_instructions\', $instructions, $marker );

    if ( $switched_locale ) {
        restore_previous_locale();
    }

    $insertion = array_merge( $instructions, $insertion );

    $start_marker = "# BEGIN {$marker}";
    $end_marker   = "# END {$marker}";

    $fp = fopen( $filename, \'r+\' );
    if ( ! $fp ) {
        return false;
    }

    // Attempt to get a lock. If the filesystem supports locking, this will block until the lock is acquired.
    flock( $fp, LOCK_EX );

    $lines = array();
    while ( ! feof( $fp ) ) {
        $lines[] = rtrim( fgets( $fp ), "\\r\\n" );
    }

    // Split out the existing file into the preceding lines, and those that appear after the marker.
    $pre_lines        = array();
    $post_lines       = array();
    $existing_lines   = array();
    $found_marker     = false;
    $found_end_marker = false;
    foreach ( $lines as $line ) {
        if ( ! $found_marker && false !== strpos( $line, $start_marker ) ) {
            $found_marker = true;
            continue;
        } elseif ( ! $found_end_marker && false !== strpos( $line, $end_marker ) ) {
            $found_end_marker = true;
            continue;
        }
        if ( ! $found_marker ) {
            $pre_lines[] = $line;
        } elseif ( $found_marker && $found_end_marker ) {
            $post_lines[] = $line;
        } else {
            $existing_lines[] = $line;
        }
    }

    // Check to see if there was a change.
    if ( $existing_lines === $insertion ) {
        flock( $fp, LOCK_UN );
        fclose( $fp );

        return true;
    }

    // Generate the new file data.
    $new_file_data = implode(
        "\\n",
        array_merge(
            $pre_lines,
            array( $start_marker ),
            $insertion,
            array( $end_marker ),
            $post_lines
        )
    );

    // Write to the start of the file, and truncate it to that length.
    fseek( $fp, 0 );
    $bytes = fwrite( $fp, $new_file_data );
    if ( $bytes ) {
        ftruncate( $fp, ftell( $fp ) );
    }
    fflush( $fp );
    flock( $fp, LOCK_UN );
    fclose( $fp );

    return (bool) $bytes;
}
}
现在你可以用你的。htaccess代码如下:

      if (is_file(ABSPATH . \'.htaccess\')) {
                $htaccess = ABSPATH . \'.htaccess\';
                $lines = array();


                $Rulesstring = \'
                <filesMatch ".(ico|pdf|flv|jpg|jpeg|png|gif|svg|js|css|swf)$">
                Header set Cache-Control "max-age=31536000, public"
                </filesMatch>
                \';

                $lines = explode( "\\n", $Rulesstring );

                insert_with_markers($htaccess, "HelloFood", $lines);
      }

相关推荐

Redirect htaccess

我需要帮助重定向我的页面。例如,我有网站https://example.com. 我需要将内容从https://example.com 到https://example.com/blog. 这不是问题,我通过更改主页URL来做到这一点。这很有效。但现在我需要添加来自旧的重定向https://example.com 到https://xmpl.com.我想用。htaccess,但这不起作用。你们能帮帮我吗?以下是我对此的一些尝试,但两者都不起作用。RewriteEngine On RewriteRu