2013-02-20 68 views
0

I`ve得到了一個網上商店,並希望使用htaccess的縮短鏈接 有3種情況的網址:htaccess的重寫URL和檢查404

shop.com/shop/18 (number) - products.php?categoryid=$1 
shop.com/shop/18/page-2 (number)/(page+number) - products.php?categoryid=$1&page=$2 
shop.com/shop/18/9877 (number)/(number) - description?categoryid=$1&productid=$2 

我嘗試

RewriteRule ^shop/?$      shop.php 
RewriteRule ^shop/(.*)/([0-9]+)/?$   description.php?categoryid=$1&productid=$2 
RewriteRule ^shop/(.*)/page-(.*)/?$   products.php?categoryid=$1&page=$2 
RewriteRule ^shop/(.*)/?$     products.php?categoryid=$1 

隨着我的嘗試 - 1(作品),2(作品),3(不工作)

  1. 我該如何重寫網址?

  2. 我該如何重定向到404頁面,例如,有沒有這樣的 類別或這樣的產品(猜測檢查與PHP和MySQL,然後 重定向)?

+0

你有什麼迄今結束?如果您至少有一些努力,我們只能提供幫助。 – 2013-02-20 13:08:57

+0

好的,加我的試試 – mozg 2013-02-20 13:17:47

回答

0

感謝,與

RewriteRule ^shop$        shop.php [L] 
RewriteRule ^shop/([0-9]+)$     products.php?categoryid=$1 [L] 
RewriteRule ^shop/([0-9]+)/(page-[0-9]+)$  products.php?categoryid=$1&page=$2 [L] 
RewriteRule ^shop/([0-9]+)/([0-9]+)$   description.php?categoryid=$1&productid=$2 [L] 
2

有很多方法可以解決這個問題;

  • 所有htaccess的(變得混亂與多個深度)
  • 聯合htaccess以及服務器端代碼

,最好的方法是適合你根據你的店是如何編碼的一個。我個人認爲,在服務器端代碼中處理它會更好,它簡化了htaccess文件,並且使您能夠更好地控制數據驗證,以及如何處理髮送的內容,到達何處以及如何處理它到達時的處理方式。

例如,在我的htaccess文件中,

<IfModule mod_rewrite.c> 
Options +FollowSymlinks 
RewriteEngine on 
# 
# Do not apply rewrite rules for non required areas 
RewriteCond %{REQUEST_URI} "/hidden-areas/" [OR] 
RewriteCond %{REQUEST_URI} "/other-areas/" 
RewriteRule (.*) $1 [L] 

# Do Not apply if a specific file or folder exists 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
# The rules on how to rewrite the urls 
RewriteRule (.*) /index.php?url=$1 [QSA,L] 
</IfModule> 

基本上,簡而言之解釋這一點,我不重寫對於特定文件夾東西,我轉發他們直上。這是爲了在外部停止對腳本的調用,或者可以毫無問題地訪問額外添加的系統。

然後,我將整個url作爲字符串轉發到我的索引頁,並處理通過使用PHP所帶來的影響,下面是一個例子。

// collect the passed url 
$url = $_GET['url']; 
// split the url into parts 
$url_parts = explode('/', $url); 
/* 
* start sorting what is what in the url 
*/ 
// count how many parts there are 
$url_parts_count = count($url_parts); 
// determine the class/module 
$class = $url_parts[0]; // generally the class/method/module depending on your system, thgough could be a category so run some checks 
// determine the last part in the array 
$last_url_part = ($url_parts_count - 1); 
// set the last part of the url to be used 
$slug = $url_parts[$last_url_part]; // generally the slug and will be empty if theres a trailing slash 
etc etc etc 

這僅僅是一個總結,我做得更多,因爲這是從CMS我寫了拍攝,但如果你希望得到你的手髒它應該給你一個很好的起點。當然,如果有必要,我很樂意進一步闡述。

當然,需要注意的是,如果你使用的是現成的,貨架系統,就應該提供給您的代碼已經)

我已經加入下面根據您的更新問題的東西,這將幫助如果你還打算去你現在的樣子:)

<IfModule mod_rewrite.c> 
    Options +FollowSymlinks 
    RewriteEngine on 
    RewriteBase/
    # 
    # Do not apply rewrite rules for non required areas 
    RewriteCond %{REQUEST_URI} "/hidden-areas/" [OR] 
    RewriteCond %{REQUEST_URI} "/other-areas/" 
    RewriteRule (.*) $1 [L] 

    # Do Not apply if a specific file or folder exists 
    # RewriteCond %{REQUEST_FILENAME} !-f 
    # RewriteCond %{REQUEST_FILENAME} !-d 
    # The rules on how to rewrite the urls 

    RewriteRule ^([a-zA-Z0-9_-]+)$ /index.php?slug=$1 [QSA,L] 
    RewriteRule ^([a-zA-Z0-9_-]+)/$ /index.php?type=$1 [QSA,L] 
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /index.php?type=$1&slug=$2 [QSA,L] 
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ /index.php?type=$1&cat=$2 [QSA,L] 
    RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ /index.php?type=$1&cat=$2&slug=$3 [QSA,L] 
</IfModule> 
+0

謝謝,你的回答幫了我很多,我也寫了我的cms :) – mozg 2013-02-20 13:44:59