htaccess:将整个子文件夹重定向到根目录 [英] htaccess: Redirect entire subfolder to root

查看:157
本文介绍了htaccess:将整个子文件夹重定向到根目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站使用Wordpress作为CMS,它已经安装在子文件夹中,但是我将其设置为显示在根域中。基本上, example.com 显示 example.com/wordpress 的内容。

My site uses Wordpress as a CMS and it's installed in a subfolder already but I set it up to display on the root domain. Basically, example.com displays the contents of example.com/wordpress.

旧的(静态)网站也在子文件夹中,我需要将整个子文件夹(文件夹和所有内容)重定向回根目录,例如将 example.com/oldwebsite 指向 example.com

The old (static) website was also in a subfolder and I need to redirect that entire subfolder (folder and all contents) back to the root - e.g. point example.com/oldwebsite to example.com.

我尝试了很多不同的方法,

I've tried lots of different approaches and I just can't get it to work.

推荐答案

以下解决方案非常容易实现。您得到的是:

The following solution is quite simple to implement. What you get is:

  • wordpress installation in a folder in the root, like in /wp
  • wp admin is accessible via http://www.yourdomain.test/wp/wp-admin/
  • site opens in root folder like: http://www.yourdomain.test/

备注


  • 如果不使用永久链接,则可能不需要.htaccess注释;我总是使用永久链接,我想每个人都应该这样做,如果您不编辑.htaccess文件(我不确定),则可能不起作用

  • 如果您不这样做。不知道您在做什么,并且您害怕弄乱事情,在开始之前先将tar / zip压缩为

  • 您可能想使用插件backwpup创建备份启动数据库之前

  • 如果可以选择服务器快照,请立即获取一个快照

  • if you don't use permalinks, you may not need the .htaccess remarks; I always use permalinks, and I think everybody should do so, and then it might not work if you don't edit the .htaccess (I'm not sure)
  • if you don't know what you're doing, and you're afraid to mess things up, tar/zip the wordpress folder before you start
  • you may want to use the plugin backwpup to create a backup of the database before you start
  • if you have the option of a snapshot of your server, take one now

需要什么才能使此工作正常运行:

What you need to get this working:


  • 如果您想要永久链接,请首先使该工作正常;这会创建或需要一个.htaccess文件

  • 将/wp/index.php复制到/index.php,以便复制到您网站的根目录

  • 如果您有.htaccess,请将其也复制到根目录中。

  • 如果您已经在根目录中具有.htaccess,则需要以某种方式进行组合;我现在假设情况并非如此

  • if you want permalinks, get that working first; this creates or needs a .htaccess file
  • copy /wp/index.php to /index.php, so to the root of your site
  • if you have a .htaccess, copy that to the root as well
  • if you have a .htaccess in the root already, you need to combine them somehow; I'll suppose for now that this is not the case

编辑/index.php

Edit /index.php


  • 添加或编辑以下行,这可能是该文件的最后一行

  • 您需要将子目录添加到您的将WordPress安装到路径

  • add or edit the following line, which is probably the last line of this file
  • you need to add the subdirectory to your wordpress installation to the path

require('./wp/wp-blog-header.php');


编辑/.htaccess

Edit /.htaccess


  • 将以下代码添加到.htaccess的末尾或创建新的.htaccess

  • 替换/ wp /以及安装wordpress的目录的名称

  • add the following code to the end of the .htaccess or create a new .htaccess
  • replace /wp/ with the name of the directory where you installed wordpress

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>
# END WordPress


WordPress设置

Wordpress settings

  • open the wordpress admin, and go to settings > general
  • edit the following two fields
  • wordpress address: http://www.yourdomain.test/wp
  • site address: http://www.yourdomain.test/
  • the wordpress address is for links to stylesheets and images, the site address is where links to other pages should go to

重命名wordpress目录

Renaming the wordpress directory


  • 如果您重命名wordpress文件夹,请首先在网站管理员中执行此操作

  • 这将更新数据库值

  • 更新后,管理员将不再工作

  • 然后将其重命名终端或ftp客户端中的目录名称

  • 然后检查所有.htaccess和index.php文件s表示旧名称,然后用新名称替换旧名称

  • if you rename the wordpress folder, do this in the site admin first
  • this updates the database value
  • after the update, the admin won't work anymore
  • then rename the directory name in the terminal or in your ftp-client
  • then check all .htaccess and index.php files for the old name, and replace the old name with the new one

这篇关于htaccess:将整个子文件夹重定向到根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆