2009-07-27 54 views
2

我發現了很多關於創建漂亮的URI和不使用mod_rewrite的東西,但是我正在尋找的是一種移除URL元素的方法(就瀏覽者而言),而不僅僅是移動物體和/或重新組織。用mod_rewrite改變站點的URL

我需要這樣的:

http://www.mysite.com/foo/bar

是這樣的:

http://www.mysite.com/bar

基本上,讓我們擺脫在URL /富的。

這可能嗎?

+0

這是什麼邏輯?這僅僅是一個控制器? – 2009-07-27 23:38:39

+0

只是爲了清理網址,真的。這是一個WordPress的安裝,它坐在public_html的dir目錄下。只是想清理乾淨。 – 2009-07-27 23:53:33

回答

1

您可以輕鬆地做到這一點,只需丟棄你不想失去什麼:

RewriteEngine On 
RewriteRule ^foo/(.*)$ $1 [R=301,NE,L] 

這應該有效地從你的URL中移除foo/。如果您不希望地址欄反映此更改,請刪除R=301。參數NE用於確保請求未被轉義。

+0

這難道不是和他想要的完全相反嗎?他希望URL爲example.com/bar,並在/ foo/bar中引用該文件。這個重寫規則似乎將example.com/foo/bar變成了/ bar中的文件。或者,也許我只是不夠了解mod_rewrite? – 2009-07-27 23:49:44

0

注意:大多數這些說明來自here, "Giving Wordpress its Own Directory"

如果是WordPress的明確,這可以通過添加像這樣到你想成爲基本目錄中的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('./blog2/wp-blog-header.php'); 
?> 

在WordPress的設置,你需要讓它知道了 「實際的」 WordPress的URL,並且希望其他人使用之間的差異:

(這些都是在「設置」我...)

另外,我不記得,如果這是自動還是沒有這樣做(我認爲這是),但如果你把對的.htaccess index.php文件將需要如下所示:

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

# END WordPress