![](/img/trans.png)
[英]Wordpress / htaccess / Mod_rewrite - need help linking to non-wordpress script
[英]Need help to replicate .htaccess mod_rewrite redirection function in php like Wordpress
我們都知道 wordpress 有簡單的 .htaccess 代碼如下 RewriteEngine on RewriteBase /
# only rewrite if the requested file doesn't exist
RewriteCond %{REQUEST_FILENAME} !-s
# pass the rest of the request into index.php to handle
RewriteRule ^(.*)$ /index.php/$1 [L]
但是,如果我將所有請求重定向到 index.php,我認為處理 php 中的每次重寫都會變得非常麻煩。 目前我有一個邏輯,比如維護一個包含所有有效重定向的數據庫表。 但我仍然不知道如何處理規則喜歡 ([0-9]+)。
如果有人以前實施過這樣的事情或有邏輯,請你指導我這件事
這樣做的唯一目的是因為我希望靈活地在我的網站菜單中添加/刪除類別。 我不想每次都從 go 到 .htaccess 並在所有地方進行編輯。 我想創建更像一個 CMS,用戶可以在其中添加刪除類別
我不明白這個問題,WordPress 已經為你處理了這一切。 除非你的意思是你沒有使用 WordPress? 在這種情況下是的,你可以這樣做。 你想要什么樣的URL結構? 你可以像這樣寫一個規則:
RewriteRule ^category/(.*)$ categories.php?cat=$1 [L]
要使 URL 像 domain.com/category/dogs 重寫到 domain.com/categories.php?cat=dogs。 顯然你可以根據自己的喜好調整它,並為標簽、帖子等編寫更多類似的規則。
在 php 中處理路由將是一個更加動態和“優雅”的解決方案。 您可以嘗試使用 CodeIgniter 之類的框架,這將自動為您管理路由並輕松定義自定義路由。 可能比寫一堆 .htaccess 規則要好。
我的建議是設置基於 php 的路由,基本思想是你做這樣的事情:
RewriteEngine On
# skip all files with extensions other than .html
# this way if you want you can append the .html to dynamic pages
# which wont really exist as a file
RewriteCond %{REQUEST_URI} \..+$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule .* - [L]
// redirect everything to your front controller
RewriteRule ^(.*)$ index.php [QSA,L]
請注意,您如何將所有 go 設置為index.php
,但您不會重寫任何變量或任何東西。 這是因為您將根據 URL 和 php 的模式計算出要做什么。 為此,您需要實現一個路由器。 基本上,路由器接受請求並將其與模式匹配,然后根據模式確定任何參數。
現有的庫可以為您執行此操作。 例如Zend_Controller_Router
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.