問題描述
有沒有任何簡單的方式來使用 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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。