2013-02-02 65 views
1

我已閱讀了有關此主題的多個主題,但他們都處理多個參數,並且我是相當新的與.htaccess我無法弄清楚如何修改答案以適應我需要的東西。.htaccess重寫php分頁

我使用.htaccess重寫網址,除了當我嘗試重寫分頁URL時,一切似乎都進行得很順利。

我的分頁網址非常簡單:blog.php?page = 2,但由於某些原因,我無法將網址重寫爲/ blog/page/2。當我嘗試將其路由到該URL時,出現「內部服務器錯誤」。現在我已將它改寫爲/ blog-page/2這沒關係,但我更願意擁有它/ blog/page/2。

我有很多我的.htaccess文件回事,所以我將只發布重寫:

RewriteEngine On 

# Full path 
#RewriteBase/

# Rewrite all php files  
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php 

# Rewrite the user profile pages 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^user/([a-zA-Z0-9_-]+)$ profile.php?user=$1 
RewriteRule ^user/([a-zA-Z0-9_-]+)/$ profile.php?user=$1 

# Rewrite the company profile pages 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^company/([a-zA-Z0-9_-]+)$ employer.php?user=$1 
RewriteRule ^company/([a-zA-Z0-9_-]+)/$ employer.php?user=$1 

# Rewrite the blog post pages 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^post/([a-zA-Z0-9_-]+)$ entry.php?id=$1 
RewriteRule ^post/([a-zA-Z0-9_-]+)/$ entry.php?id=$1 

# Rewrite the job listing pages 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^job/([a-zA-Z0-9_-]+)$ listing.php?id=$1 
RewriteRule ^job/([a-zA-Z0-9_-]+)/$ listing.php?id=$1 

# Rewrite blog pagination 
RewriteRule ^blog/page/([0-9]+)$ blog.php?page=$1 
RewriteRule ^blog/page/([0-9]+)/$ blog.php?page=$1 

很顯然,我遇到的問題是與最後重寫規則對博客的分頁。爲什麼我不能做到/ blog/page/2,但是我可以做到/ blog-page/2?

我也試圖做一個多頁.htaccess重寫博客類別和工作類別,但我不斷收到一個內部錯誤。我想爲博客重寫1個工作類別和博客類別。這是我有什麼:

# Rewrite the category queries 
RewriteRule ^/category/(.*)/?$ /$.php?cat=$1 [L,QSA] 

是否有可能使此分頁重寫工作的2個不同的頁面?比如blogs.php和jobs.php?

另外,是否有必要在制定重寫規則之前繼續使用RewriteCond %{REQUEST_FILENAME} !-f?還是一次足夠?

+0

在重寫規則RewriteRule^blog/page /([0-9] +)之前,嘗試將此行添加到最後一個規則集:'RewriteCond%{REQUEST_URI}!blog \ .php [NC] )$ blog.php?page = $ 1'等。 –

+0

沒有工作,但這是一個好主意。我仍然收到「內部服務器錯誤」 –

+0

您是否嘗試刪除該規則集以查看錯誤是否持續? –

回答

2

您是否檢查過apache錯誤日誌以獲取有關錯誤消息的信息?如果沒有,那麼只要您收到500條內部錯誤消息,這就是首先啓動的地方。

請看apache手冊中的RewriteCond。看來你期望RewriteCond能夠爲多個規則工作,但這不是它的工作原理。 RewriteCond只適用於下一個RewriteRule。您可以將多個RewriteCond行鏈接在一起以獲得單個RewriteRule,但如果您想要將RewriteCond應用於RewriteRules,則必須將其放在每個RewriteRules之前。

好消息是,大部分的規則不需要的RewriteCond:

RewriteEngine On 

# Rewrite all php files  
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php [L] 

# Rewrite the user profile pages 
RewriteRule ^user/([a-zA-Z0-9_-]+)/?$ profile.php?user=$1 [L] 

RewriteCond 
# Rewrite the company profile pages 
RewriteRule ^company/([a-zA-Z0-9_-]+)/?$ employer.php?user=$1 [L] 

# Rewrite the blog post pages 
RewriteRule ^post/([a-zA-Z0-9_-]+)/?$ entry.php?id=$1 [L] 

# Rewrite the job listing pages 
RewriteRule ^job/([a-zA-Z0-9_-]+)/?$ listing.php?id=$1 [L] 

# Rewrite blog pagination 
RewriteRule ^blog/page/([0-9]+)/?$ blog.php?page=$1 [L] 

我做了一些修改你的規則:

  1. 刪除大部分的RewriteCond規則。如果您打算在任何目錄中都有一些靜態文件,則需要添加RewriteCond %{REQUEST_FILENAME} !-f規則以供它們投放。
  2. 合併規則以處理尾隨/添加一個?使斜線可選。
  3. 在您的規則中添加了[L],表示在找到並執行匹配後不需要運行其他規則。請參閱apache手冊中的RewriteRule以獲取有關可用標誌的更多詳細信息。
+0

我將你的代碼複製並粘貼到我的.htaccess文件中,除了分頁URL仍然可以工作外,所有東西似乎都可以工作。我去'會員/ page/2',我得到一個內部服務器錯誤...任何想法爲什麼? –