问题描述
有没有任何简单的方式来使用 wordpress,php 或 apache 将所有涉及/wp-admin
的网址重写为/admin
?
我已将此条目添加到.htaccess 文件中:
RewriteRule ^admin /wp-login.php [L]
但是,尽管 domain.com/admin
将正确显示登录页面,您登录后的所有页面都将显示为/wp-admin
,我想始终显示/admin
。
有什么想法吗?
最佳解决方案
通过 mod_rewrite(用于处理 URL 重写的 apache 模块) 来解答您作为 apache URL 布局配置的问题,这可能会有所帮助:
RewriteRule ^admin/(.*)$ /wp-admin/$1 [QSA,L]
(未经测试的配置指令,检查文档以防万一发生错误)
这将基本上将/admin /… 的任何请求转换为/wp-admin /… 。
如果这只是你想知道的唯一的事情,我必须承认,这对于 worpdress @ se 来说这是一个很小的 off-topic,因为这是一个如何配置 apache 网络服务器的问题。一个更好的地方可能是 serverfault 。
请记住,这只会重写所请求的 URL 。 WordPress 一无所知,它仍然输出到/wp-admin /在管理员上的链接。您可能想要重写的任何管理 URL 都有一个钩子:
return apply_filters('admin_url', $url, $path, $blog_id);
您将需要用 domain/admin/
替换 domain/wp-admin/
部件,并使用自己的过滤器。
次佳解决方案
hakre …. 很好的答案… 现在用最新版本的 wordpress 有一个单独的网络管理员我使用你的解决方案,并添加到部分正确重写”Network Admin” 链接以相同的方式…
/**
* 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;
}
}
第三种解决方案
除了以前的解决方案。我发现缺少的 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]
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。