簡體   English   中英

刪除PHP擴展名並使用.htaccess重寫查詢字符串

[英]Removing PHP extension and re-writing query string with .htaccess

我正在嘗試重組現有網站,同時將URL重新編寫為更用戶友好的格式。 我現在擁有的是http://example.com/test_vars.php?test_var1=value1&test_var2=value2 ,理想情況下,我想要的是http://example.com/test_vars/test_var1/value1/test_var2/value2

誠然,我是修改htaccess文件的新手,但是已經完成了我的研究並嘗試了一些方法。 我最接近的是我要尋找的東西(誠然,距離不是很近),但是我在下面附上了代碼:

Options +FollowSymLinks
Options +Indexes
RewriteEngine on
DirectoryIndex index.html

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(([^/]+/)*[^.]+)$ /$1 [L]

# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.html
RewriteRule .* index.html

這樣,我最終得到的URL為http://example.com/test_vars.php/test_var1/value1/test_var2/value2但是沒有任何變量傳遞到頁面。

在您的test_vars.php ,您必須手動解析參數。 $_SERVER['REQUEST_URI']將包含/test_var1/value1/test_var2/value2 例如,編寫一個簡單的函數以使用explode()解析字符串。

要使您的網址不帶“ .php”擴展名,請更改此行

RewriteRule ^(([^/]+/)*[^.]+)$ /$1 [L]

對此:

RewriteRule ^test_vars test_vars.php

現在您有了漂亮的URL: http : //example.com/test_vars/test_var1/value1/test_var2/value2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM