如何重定向/重写所有/wp-登录请求

时间:2010-11-14 作者:NetConstructor.com

有没有简单的方法让wordpress、php或apache重写所有涉及/wp-admin/admin?

我已将此条目添加到。htaccess文件:

RewriteRule ^admin /wp-login.php [L]
然而,尽管domain.com/admin 将正确显示登录页面登录后的所有页面将URL显示为/wp-admin 我想一直展示给大家看/admin.

对此有何想法?

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

通过以下方式以apache URL布局配置的形式回答您的问题:mod_rewrite (处理URL重写的apache模块),这可能会有帮助:

RewriteRule ^admin/(.*)$ /wp-admin/$1 [QSA,L]
(未测试的配置指令,请检查文档以防出现错误)

这基本上会将任何请求转换为/admin/。。。输入/wp admin/。

如果这是你唯一想知道的事,我必须承认这有点离题worpdress@se因为这实际上是一个如何配置apache Web服务器的问题。更好的地方可能是serverfault.

请记住,这只会重写请求的URL。Wordpress对此一无所知,它仍然会在管理员上输出指向/wp admin/的链接。对于任何管理员URL,都有一个钩子,您可能还想重写它:

return apply_filters(\'admin_url\', $url, $path, $blog_id);
您需要更换domain/wp-admin/ 放弃domain/admin/ 自带过滤器。

SO网友:gruvii

哈克雷。。。。很好的回答。。。现在,wordpress的最新版本有了一个单独的网络管理员,我使用了您的解决方案,并在部分中添加了以相同方式正确重写“网络管理员”链接的内容。。。

    /**
     * Change Admin URL
     * 
     * Copyright (C) 2010  hakre <http://hakre.wordpress.com/>
     * 
     * This program is free software: you can redistribute it and/or modify
     * it under the terms of the GNU Affero General Public License as
     * published by the Free Software Foundation, either version 3 of the
     * License, or (at your option) any later version.
     * 
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU Affero General Public License for more details.
     * 
     * You should have received a copy of the GNU Affero General Public License
     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     * 
     * USAGE: 
     * 
     * Copy the file into  wp-content/mu-plugins  directory and add the 
     * following RewriteRule to your apache configuration or .htaccess:
     * 
     *  RewriteRule ^admin/(.*)$ wp-admin/$1 [QSA,L]
     * 
     * It will rewrite the wordpress admin-URL 
     *  
     *   from: http://example.com/wp-admin/ ...
     *   to  : http://example.com/admin/ ...
     * 
     * @author hakre <http://hakre.wordpress.com>
     * @see http://wordpress.stackexchange.com/questions/4037/how-to-redirect-rewrite-all-wp-login-requests/4063
     * @todo mod_rewrite_rules - filter to insert into .htacces on plugin activation
     * 
     */
    /** Updated version my Mark Figueredo, <http://gruvii.com/> **/
    return ChangeAdminUrlPlugin::bootstrap();

    class ChangeAdminUrlPlugin {
     private $renameFrom = \'wp-admin\';
     private $renameTo = \'admin\';
     static $instance;
     static public function bootstrap() {
      null === self::$instance
       && self::$instance = new self()
       ;
      return self::$instance;
     }
     private function setCookiePath() {
      defined(\'SITECOOKIEPATH\') || define(\'SITECOOKIEPATH\', preg_replace(\'|https?://[^/]+|i\', \'\', get_option(\'siteurl\') . \'/\' ) );
      defined(\'ADMIN_COOKIE_PATH\') || define(\'ADMIN_COOKIE_PATH\', SITECOOKIEPATH . $this->renameTo);
     }
     public function __construct() {
      $this->setCookiePath();
      add_action(\'init\', array($this, \'init\')) ;
     }
     public function init() {
      add_filter(\'admin_url\', array($this, \'admin_url\'), 10, 3);
      add_filter(\'network_admin_url\', array($this, \'network_admin_url\'), 10, 3);//Added by Mark Figueredo, <http://gruvii.com/>
     }
     public function admin_url($url, $path, $blog_id) {
      $renameFrom = $this->renameFrom;
      $renameTo = $this->renameTo;
      $scheme = \'admin\';
      $find = get_site_url($blog_id, $renameFrom.\'/\', $scheme);
      $replace = get_site_url($blog_id, $renameTo.\'/\', $scheme);
      (0 === strpos($url, $find))
       && $url = $replace.substr($url, strlen($find))
       ;
      return $url;
     }
// Added by Mark Figueredo, <http://gruvii.com/> 
     public function network_admin_url($url, $path) {
      $renameFrom = $this->renameFrom;
      $renameTo = $this->renameTo;
      $scheme = \'admin\';
      $find = network_site_url($renameFrom.\'/\', $scheme);
      $replace = network_site_url($renameTo.\'/\', $scheme);
      (0 === strpos($url, $find))
       && $url = $replace.substr($url, strlen($find))
       ;
      return $url;
     }
    }

SO网友:coderex

除了以前的解决方案之外。我找到了丢失的css和脚本文件。

解决方法是

RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

# Fix using the previous answer. 
RewriteRule ^admin/(.*)$ wp-admin/$1 [QSA,L] 

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule  ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]

#Fix to load css js image files for multi site admin urls. 
#old code RewriteRule  ^[_0-9a-zA-Z-]+/(.*\\.php)$ $1 [L]
RewriteRule  ^[_0-9a-zA-Z-]+/(.*\\.(php|css|js|png|jpg|gif))$ $1 [L]

RewriteRule . index.php [L]

结束

相关推荐

WP-ADMIN似乎正在重定向

我的w-admin登录有一个奇怪的问题。这是从我升级到3.0以后才开始的,当我转到wp admin时,登录表单显示正常,但当我输入用户名并通过时,每次都会再次显示登录表单。使用密码恢复功能会导致电子邮件未找到错误。我知道用户名密码和电子邮件是正确的,b/c我可以访问mysql数据库,我可以看到值(至少用户名和电子邮件) 有人知道会出什么问题吗