mod_rewrite มี %26 (&) ใน path มีปัญหาว่า .htaccess มันส่งค่าไม่ครบ มัน decode &

เริ่มโดย ohmohm, 04 ตุลาคม 2009, 20:18:03

หัวข้อก่อนหน้า - หัวข้อถัดไป

0 สมาชิก และ 1 ผู้มาเยือน กำลังดูหัวข้อนี้

ohmohm

คือเขียน .htaccess เพื่อมารองรับ url แบบที่ path มันได้มาจาก rawurlencode() มาแล้ว ทำให้เครื่องหมาย & ถูกแปลงเป็นไปเป็น %26

/page/ben%20%26%20jerry คือ ben & jerry

ปัญหาคือพอจะใช้ .htaccess เพื่อเรียกหน้า index.php?q=ben%20%26%20jerry
RewriteRule ^page/([^/]+)/?$ index.php?p=$1 [L]

มันไม่ส่งข้อความตั้งแต่ & ไปด้วย ( ได้แค่ ben ) สืบไปสืบมาจริงๆ .htaccess มันส่ง แต่มันแปลง %26 ไปเป็นเครื่องหมาย & แล้วส่ง มันเลยกลายเป็นตัวแปรใหม่ ใน $_GET[]

เลยหาทางแก้ เจอแนวทางว่า ให้ทำเป็นสองบรรทัด แบบแรก ต้องไม่มีเครื่องหมาย & ให้ส่งไปได้เลย
อีกแบบ ถ้ามี & ให้แบ่งเป็นสองตัว แล้วส่งสองตัว
RewriteRule ^page/([^/&]+)/?$ index.php?p=$1 [L]
RewriteRule ^page/([^/]+)\&([^/]+)/?$ index.php?p=$1%26$2 [L]


ก็แก้ปัญหาได้ แต่ได้แค่ & แบบมีตัวเดียว ถ้ามากกว่านี้ ทำไม่ได้
ใครมีแนวทางแก้แบบอื่นมั่ง ช่วยคิดที


Tee++;

packed ไปเลยครับ

$values = base64_encode(serialize($_POST));

ตอนใช้ก็

$values = unserialize(base64_decode($_GET['xxxx']));
[direct=http://laravel.in.th]Laravel in Thai[/direct]
[direct=http://jquerytips.com]jQueryTips by Tee++;[/direct]
[direct=https://www.facebook.com/jQueryTips]jQueryTips Page[/direct]
[direct=https://www.facebook.com/Laravel.in.th]Laravel in Thai Page[/direct]
[direct=https://twitter.com/Teepluss]Teepluss's Twitter[/direct]


ohmohm

ขอบคุณครับ เดี๋ยวไปทดลอง แต่ url มันคงเปลี่ยนไปแบบเข้ารหัสอ่านไม่รู้เรื่องแน่ๆเลย

Tee++;

อ้า โทษทีครับ ลืมไปว่ามันเป็น .htaccess ไ่ม่ได้ส่ง query แบบปกติ

แบบนี้ผมว่าใช้เป็น slug post ดีกว่านะครับ

ben-jerry

แต่ต้องทำงานหนักขึ้นหน่อยคือ ตรง โพส ก็ต้องไปสร้าง slug เหมือนกัน

ไม่งั้นก็ใช้ slug ทางเดียวแล้ว ใช้ no query เอา แบบ

/id-213-ben-jerry แบบนี้เอาได้มั้ยครับ

[direct=http://laravel.in.th]Laravel in Thai[/direct]
[direct=http://jquerytips.com]jQueryTips by Tee++;[/direct]
[direct=https://www.facebook.com/jQueryTips]jQueryTips Page[/direct]
[direct=https://www.facebook.com/Laravel.in.th]Laravel in Thai Page[/direct]
[direct=https://twitter.com/Teepluss]Teepluss's Twitter[/direct]


ball6847

ลองใช้ ; เป็น arg_separator ได้มั้ยครับ

php_value arg_separator.input ";"

แต่ก็ใช้ & แบ่งแบบปกติไม่ได้นะคับ

Note - จาก PHP Manual ครับ

อ้างถึง
Be careful about variables that may match HTML entities. Things like &amp, &copy and &pound are parsed by the browser and the actual entity is used instead of the desired variable name. This is an obvious hassle that the W3C has been telling people about for years. The reference is here: » http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2.

PHP supports changing the argument separator to the W3C-suggested semi-colon through the arg_separator .ini directive. Unfortunately most user agents do not send form data in this semi-colon separated format. A more portable way around this is to use & instead of & as the separator. You don't need to change PHP's arg_separator for this. Leave it as &, but simply encode your URLs using htmlentities() or htmlspecialchars().

ผมว่าตัด & ไปเลยน่าจะเข้าใจง่ายกว่านะคับ
ผมก็เพิ่งจะรู้เหมือนกันว่า apache มันดัน parse แบบที่ decode แล้วมาให้เรา
We use Ubuntu.

[direct=http://ng-seo.sourcelab.xyz/]AngularJS SEO Experimental[/direct]

ohmohm

โอ้ ความรู้ๆ เลยครับ แต่เปลี่ยนใจมาทำง่ายๆ ดีกว่า

อย่างรายนี้ ล่อไป 3 ampersands
http://www.webmasterworld.com/apache/3655433.htm
แล้วก็มีคนอธิบายว่า uri ที่มี & มันขัดกับ RFC2396 แต่จะว่าไป เราใส่ %26 ลงไปนะ

เหมือนจะเคยเห็นว่า ให้ .htaccess ไปเรียก perl ได้ แต่คงยุ่งยากเกินไปหน่อยละ

updated:
อันนี้แนะนำให้ใช้ $_SERVER['SCRIPT_URI'] กับ $_SERVER['PATH_INFO'] http://www.dracos.co.uk/code/apache-rewrite-problem/