2012-03-31 208 views
0

我的移動網站(微小的定製CMS由WordPress的啓發)有一個主頁,博客網頁,新聞網頁,一個關於頁面和聯繫人頁面,./blog./news./about./contact是實際的文件夾。 主頁博客 and 新聞分頁。 聯繫人是單頁,沒有分頁。多的htaccess重寫規則

我需要htaccess重寫規則來做以下(A)和(B)。我只用(A)取得了進展,並且htaccess文件位於網站根目錄中。

(A)

  • m.website.com/index.php?page=1重寫到m.website.com
  • m.website.com/index.php?page=2重寫到m.website.com/page/2
  • m.website.com/blog/index.php?page=1重寫到m.website.com/blog
  • m.website.com/blog/index.php?page=2重寫到m.website.com/blog/page/2
  • m.website.com/news/index.php?page=1重寫到m.website.com/news
  • m.website.com/news/index.php?page=2重寫到m.website.com/news/page/2

問題: 下面是我用什麼上面,但我只得到它的工作爲主頁現在爲。我不知道如何結合規則,包括博客新聞頁也。此外,它複製我的鏈接,因爲m.website.comm.website.com/page/1都在使用中。我需要擺脫/page/1無處不在。分頁應該從第2頁開始。我試圖用RedirectMatch擺脫它,但它不起作用,所以我評論了它。

RewriteEngine On 
RewriteBase/
RewriteRule ^page/(.*) index.php?page=$1 [NC] 
#RedirectMatch 301 ^/page/1/$ http://m.website.com/ 

(B)

  • 我已經有它接受簡潔的URL並返回其postIDs在家裏
  • 的讀更多鏈接,每篇文章一個permalink.php文件,博客或新聞頁面將具有格式http://m.website.com/2012/03/the-post-title
  • 點擊後,htaccess將查詢permalink.php字符串/2012/03/the-post-title以獲取postID,然後打開http://m.website.com/article.php?id=postID地址欄中的地址將爲http://m.website.com/2012/03/the-post-title,並且這顯示文章完整,無論是主頁,博客頁面還是新聞頁面。

問題:我一直在尋找,我不完全知道如何去(B)之上,但我知道這是可能的。最後,A和B的所有規則將位於站點根目錄中的相同htaccess文件中。

謝謝

+0

最乾淨的解決辦法是僅使用重定向是不是靜態的資源,以你的PHP腳本,然後做所有的URL解析等 – ThiefMaster 2012-03-31 23:11:21

+0

@ThiefMaster感謝所有請求使用一個重寫規則。但是主頁,博客頁面和新聞頁面有3個PHP文件(index.php),它們有不同的查詢和內容。單個重寫規則將如何工作? – Cogicero 2012-03-31 23:14:19

+0

通過另一個PHP文件路由一切,然後包含正確的文件。 – ThiefMaster 2012-03-31 23:14:54

回答

2

這應該對你有幫助。看起來你已經完成了大部分工作,所以不需要做任何事情。

RewriteEngine On 
RewriteBase/

# you do not need a rewrite for the root as you should have index.php 
# as your default document and page 1 as your default page 

RewriteRule ^page/1/?$     /[NC,L,R=301] 
RewriteRule ^page/(\d+)/?$    index.php?page=$1 [NC,L] 

# you do not need a rewrite for blog as you should have index.php as your 
# default document and page 1 as your default page 

# you do not need a rewrite for news as you should have index.php as your 
# default document and page 1 as your default page 

RewriteRule ^(blog|news)/page/1/?$  $1 [NC,L,R=301] 
RewriteRule ^(blog|news)/page/(\d+)/?$ $1/index.php?page=$2 [NC,L] 

###################################################################### 
################## ADD YOUR PERMALINK.PHP CODE HERE ################## 
###################################################################### 

UPDATE 爲了有效地打開/ 2012/03 /該標題後進入一個帖子ID,你需要能夠讓你的數據庫爲你做的,因爲它是知道的唯一的事回答。所以,你可以使用一個RewriteMaphttp://httpd.apache.org/docs/2.3/rewrite/rewritemap.html#dbd我從來沒有做過這個我自己,我會建議反對。不是因爲我知道有與它一個問題,我只是有一個壞感覺它。

的替代和一個我會冠軍是剛剛做這樣的事情: -

RewriteRule ^(\d{4})/(\d{2})/([^/]+))/?$  article.php?postIdentifier=$1/$2/$3 [L] 

然後在article.php擊中數據庫,並使用在postIdentifier的信息以獲得正確的文章。

有意義嗎?

+0

嗨Baynezy,謝謝!我想測試並接受這一點,但上述內容似乎只解決了(A)我的問題的一部分。至於B部分,我該如何將漂亮的URL永久鏈接重寫到postID中?如果您需要澄清問題,請告訴我。 – Cogicero 2012-04-01 08:55:45

+0

@Cogicero請看我的更新 – baynezy 2012-04-01 12:46:14

+0

謝謝Baynezy!我現在就試試看。我意識到我犯了一個錯誤。該應用程序是一個子文件夾'/應用/主站點的demo',所以纔出現了RewriteBase不能/(或應該是什麼?)我想'RewriteBase /應用/演示/'但是所有的重寫不工作。你能在這裏,在評論中給我建議嗎?我認爲這應該起作用,以後會接受。 – Cogicero 2012-04-01 13:03:37