2012-01-22 72 views
0

如何翻譯就像一個網址: http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAILmodrewrite網站,在.htaccess斜線

到: http://localhost/mySite/OFFERS/ARTICLE/DETAIL

如果一個參數爲空,它應該仍然工作是這樣的: 本地主機/ MYSITE /?鏈接=優惠&子鏈路=文章 本地主機/ MYSITE /信息/條

額外的問題:有下的index.php的進入頁面和重寫應與index2.php工作。最好的辦法是,如果它能在localhost和live系統下工作而不做任何修改。

目前我正在使用:RewriteRule ^([^ /。] +)$ index2.php?link = $ 1 [L] 但是,這隻適用於一個參數,我無法改進這個代碼的年齡^^

+0

你是什麼意思進入頁面?你的意思是進入網站/離開網站? – ThinkingMonkey

+0

只是重定向不適用於index.php - >不僅適用於/?link = asdf。 但爲/index2.php?link=asdf index.php只是一個內部鏈接的歡迎頁面 – CodingYourLife

+0

我還是不明白。你的意思是你想要將'index.php /?link = asdf'或'/?link = asdf'重定向到'/index2.php?link = asdf'? – ThinkingMonkey

回答

0
RewriteRule ^([^/.]+)(/([^/.]+))?(/([^/.]+))?$ index2.php?link=$1&sublink=$3&subsublink=$5 [L,QSA] 

注意localhost/mySite/OFFERS/ARTICLE鏈接localhost/mySite/?link=OFFERS&sublink=ARTICLE&sussublink=而不是localhost/mySite/?link=OFFERS&sublink=ARTICLE。 不應該是一個大問題,但要確保PHP代碼不是我們的isset($_GET['subsublink'])

+0

工作!萬分感謝!正是我在找什麼! – CodingYourLife

0

嘗試將以下內容添加到您網站的mysite目錄中的htaccess文件中。

RewriteEngine on 
RewriteBase /mysite/ 

# rewrite http://localhost/mySite/OFFERS/ARTICLE/DETAIL to 
# http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL 
#assumes that index2.php is in the root directory of site 
RewriteRule ^([-a-zA-Z0-9])+/([-a-zA-Z0-9])+/([-a-zA-Z0-9])+ index2.php?link=$1&sublink=$2&subsublink=$3 [NC,L] 

#redirect index to index2 
#if you do not want to redirect, just server rewrite, remove the R=301 below 
RewriteRule ^index\.php$ index2.php [NC,L,R=301] 
+0

我試圖讓這個代碼工作,但只得到一個404錯誤...索引,索引2和.htaccess文件在xampp/htdocs/mysite/ – CodingYourLife

+0

@yourmother確定編輯以反映一切在'mysite'目錄中。 –

+0

localhost/mysite/OFFERS上的404錯誤。如果我修改的代碼只適合一個參數而不是三個,那麼站點顯示但看起來非常可怕(圖像修補和文件修改似乎不能正常工作) – CodingYourLife