我正在設計我的應用程序。我應該做下一件事。所有GET參數(?var = value)在mod_rewrite的幫助下應該轉換爲/ var/value。我怎樣才能做到這一點?我只有1個.php文件(index.php),因爲我正在使用FrontController模式。你能幫我用這個mod_rewrite規則嗎?
對不起,我的英語。先謝謝你。PHP的所有GET參數與mod_rewrite
回答
我在使用'seo-friendly'網址的網站上做這樣的事情。
在.htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]
然後在index.php文件:
if ($_SERVER['REQUEST_URI']=="/home") {
include ("home.php");
}
的的.htaccess規則告訴它加載的index.php,如果要求的文件或目錄未找到。然後你只需解析請求URI來決定index.php應該做什麼。
美麗。這正是我需要的。 – Qix 2013-05-01 16:09:27
AFAIK mod_rewrite在問號後不處理參數 - 重寫規則的regexp行結束符與'?'之前的路徑末尾相匹配。所以,你幾乎不能傳遞參數,或者在重寫時完全放棄參數。
的.htaccess
RewriteEngine On
# generic: ?var=value
# you can retrieve /something by looking at $_GET['something']
RewriteRule ^(.+)$ /?var=$1
# but depending on your current links, you might
# need to map everything out. Examples:
# /users/1
# to: ?p=users&userId=1
RewriteRule ^users/([0-9]+)$ /?p=users&userId=$1
# /articles/123/asc
# to: ?p=articles&show=123&sort=asc
RewriteRule ^articles/([0-9]+)/(asc|desc)$ /?p=articles&show=$1&sort=$2
# you can add /? at the end to make a trailing slash work as well:
# /something or /something/
# to: ?var=something
RewriteRule ^(.+)/?$ /?var=$1
第一部分是接收到的URL。第二部分重寫的URL,您可以使用$_GET
讀出。 (
和)
之間的所有內容都被視爲一個變量。第一個將是$1
,第二個是$2
。這樣,您就可以確定變量在重寫的URL中的確切位置,從而知道如何檢索它們。
通過使用(.+)
,您可以保持一般性並允許「一切」。這僅僅意味着:任何字符的一個或多個(+
)(.
)。或者更具體而且例如僅允許數字:[0-9]+
(0到9範圍內的一個或多個字符)。你可以在http://www.regular-expressions.info/找到更多有關正則表達式的信息。這是一個很好的網站來測試它們:http://gskinner.com/RegExr/。
您的.htaccess中的以下代碼將重寫您的URL從例如。 /api?other=parameters&added=true
至/?api=true&other=parameters&added=true
RewriteRule ^api/ /index.php?api=true&%{QUERY_STRING} [L]
這就是我正在尋找的!謝謝! – Aaron 2013-04-13 21:13:37
- 1. PHP - mod_rewrite的與參數
- 2. 的mod_rewrite和GET參數
- 3. 重寫PHP所有GET參數的.htaccess
- 4. PHP獲得與GET參數
- 5. Php GET參數與表格
- 6. mod_rewrite的,乾淨的URL和GET參數
- 7. 的mod_rewrite上可選的GET參數
- 8. mod_rewrite的隱藏URL GET參數
- 9. 所有url上的PHP mod_rewrite
- 10. 的HTTP GET與參數,PHP函數
- 11. 問題與mod_rewrite的和GET
- 12. 在mod_rewrite中傳遞GET參數
- 13. 變化GET參數和使用mod_rewrite
- 14. GrizzlyHttpServerFactory.createHttpServer @GET與參數
- 15. 的Apache的mod_rewrite和PHP GET陣列
- 16. Apache的mod_rewrite和PHP?參數=東西
- 17. mod_rewrite的htaccess的與逃脫參數
- 18. 的mod_rewrite規則與get方法
- 19. 我還可以使用PHP變量GET與mod_rewrite的
- 20. PHP多語言GET參數
- 21. Slim PHP和GET參數
- 22. .htaccess RewriteRule php-get參數
- 23. mod_rewrite只在GET
- 24. 語言參數改寫與mod_rewrite的
- 25. Mod_rewrite將所有內容轉發到index.php的參數
- 26. 沒有GET的URL參數
- 27. CakePHP2與GET參數分頁
- 28. REST GET動詞與參數
- 29. 與參數GET操作
- 30. Rails:link_to與塊和GET參數?
這聽起來對您和您的用戶都很可怕。通常,使路徑看起來像指定內容是個不錯的主意,但只保留其他參數。例如,您可能不是/content.php?id=5,而是/ content/5。如果你有一些選擇的東西,但你可能想要做一些像/ content/5?opt1 = something&opt2 = something。 – Brad 2011-01-12 21:06:28