简体   繁体   English

否定单词的htaccess的特定重写规则

[英]Specific rewrite rule on htaccess with negation of word

I was trying to rewrite or redirect a URL with a regular expression based on the negation of a word. 我试图根据单词的取反使用正则表达式重写或重定向URL。

The scenario is as following: 场景如下:

I have a wordpress installed on example.com/wp-blog but the visualization is through API on example.com/blog . 我在example.com/wp-blog安装了wordpress,但是可视化是通过example.com/blog API实现的。 I need to disable the access to /wp-blog and redirect it to /blog but preserving the access to /wp-blog/wp-admin . 我需要禁用对/wp-blog的访问并将其重定向到/blog但是保留对/wp-blog/wp-admin

I try the following condition on htaccess: 我在htaccess上尝试以下条件:

RewriteCond ^wp-blog/((?!wp-admin).)*$ [NC]
RewriteRule ^(.*)$ /blog/$1  [R=301,L]

But with no success. 但是没有成功。

I also try this: 我也尝试这样:

Redirect 301 ^wp-blog/((?!wp-admin).)*$ /blog/$1

And also this: 还有这个:

RewriteRule ^wp-blog/wp-admin $ /wp-blog/wp-admin $1 [L,NC]
RewriteRule ^wp-blog/wp-login.php $ /wp-blog/wp-login.php $1 [L,NC]
RewriteRule ^wp-blog/(?!.*wp-admin)$ /blog/$1 [R=301,L,NC]

I have no success with all combinations I try. 我尝试的所有组合都没有成功。 So how can I redirect/rewrite /wp-blog to /blog but with access to /wp-blog/wp-admin ? 那么,如何将/wp-blog重定向/重写到/blog但可以访问/wp-blog/wp-admin

Thank you so much. 非常感谢。

You could use request_uri to test if following path is wp-admin then write a rule to rewrite every thing to /blog : 您可以使用request_uri来测试以下路径是否为wp-admin,然后编写一条规则以将所有内容重写为/blog

RewriteCond %{REQUEST_URI} ^/?wp-blog(?!/wp-admin)
RewriteRule ^/?wp-blog(/.*)? blog$1 [R=301,L]

No need to do anything with .htaccess, all you can be done with WordPress directly. 无需使用.htaccess进行任何操作,您可以直接使用WordPress完成所有操作。

its can be done in two step 它可以分两步完成

Step 1 : on your root create index.php with below content 第1步:在您的根目录下,使用以下内容创建index.php

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog/wp-blog-header.php' ); /** here wp-blog is place as your folder name */

Step 2 : now change "staging.domain.com" to "staging.domain.com/wp-blog" in your database ( for this download sql > open & edit for replace > upload new sql ). 步骤2:现在将数据库中的“ staging.domain.com”更改为“ staging.domain.com/wp-blog”(对于此下载sql>打开并编辑替换>上传新sql)。

Now you full site run on staging.domain.com URL but files are call from "wp-blog" folder. 现在,您的完整站点都在staging.domain.com URL上运行,但是文件是从“ wp-blog”文件夹中调用的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM