หากจะทำ Rewrite URL หรือ การทำ permalinks ใน WordPress
หากใช้ Nginx เป็น reverse proxy
1 2 3 4 5 6 7 8 9 10 11 | location / { try_files $uri $uri/ /index.php?$args; } location ~\.php$ { proxy_pass https://xxxxxxx:8080$request_uri; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } |
ดูให้ดี พ่วง ?$args; เพื่อรับ args ต่อท้าย Index.php ไปด้วย
หลังจากกระบวนการข้างบน Request จะถึงส่งต่อไปยัง :8080 ซึ่งเราอาจจะรัน Apache ไว้ จริงๆไม่อาจจะหรอก รันเหอะ
ซึ่งสิ่งที่จะทำให้ Rewrite สำเร็จนั้นคือต้องมีไฟล์ .htaccess และ อย่าลืมเปิด Mod ใน Apache
.htaccess
1 2 3 4 5 6 7 8 9 10 | # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress |
Mod ของ Apache นั้นชื่อ “mod_rewrite”
วิธีการปรับ มีหลากหลายวิธีแต่ถ้าใช้ Ubuntu จัดการ
1 | a2enmod rewrite |
บรรทัดเดียวชีวิตจบ
จากนั้น Restart ทั้งสอง Service เพื่อความแฮปปี้
1 2 | service apache2 restart service nginx reload |
เย๊ เลิกปวดหัว กับปัญหา 404 ของwordpress เวลาทำ permalinks ได้แล้ว!