Tag Archive url

Byphunsanit

Apache: redirect

บางครั้งเว็บก็ทำเรื่องเฉพาะกิจอย่างมีกิจกรรมพิเศษช่วงปีใหม่ ก็ใช้วิธี redirct ไป subdomain ก่อนชั่วคราว หรือเปลี่ยนเพิ่มลด URL ทำได้โดยแก้ config

  1. sudo nano /etc/apache2/sites-available/example.com.conf
  2. พิมพ์เพิ่มเติมตามตัวอย่าง โดย
    • 301 Moved permanently : การย้าย URL ไปอยู่ที่ตำแหน่งใหม่อย่างถาวร ให้ GOOGLE จำไว้ว่าทุกลิงค์ที่เคยใช้อยู่เดิม ให้เปลี่ยนเป็นตัวใหม่
    • 302 Found : ใช้ชั่วคราว
    • 307 – Temporary redirect : ใช้ชั่วคราวชั่วคราวตอนนี้  คล้ายกับ 302 แต่จะกลับไปใช้ URL เก่านะ
    • 410 – Content deleted : ลิงค์นี้ ลบออกไปแล้วนะ
    • 451 – Content unavailable for legal reasons :URL นี้ไม่แสดงภายใต้เหตุผลทางกฎหมาย

ดูความหมายประเภทของ Redirect เพิ่มเติมได้ที่ http://Which redirect should I use?

<VirtualHost *:80>
    Redirect 302 / "http://newyear.example.com/"
    ServerName www.pexample.com
</VirtualHost>
  1. รีสตาร์ apache
    sudo systemctl restart apache2
  2. เรียก url ดูถ้าไม่ทำงานให้เช็ดดูตรง ServerAlias และ ServerName ใน config ทั้งหมด เพราะว่าอาจจะทำให้ apache สับสนไปทำงานผิดที่ได้ ให้ใส่ # ไปหน้าบรรทัดเดิมที่คิดว่าเป็นสาเหตุเพื่อ comment ออกไปชั่วคราว restart apache แล้วดูผลอีกครั้ง
Byphunsanit

rewrite url ใน iis

โดยปกติ PHP จะใช้ Apache HTTP Server เป็น server ถ้าต้องการให้ URL เรียบร้อยสวยงามก็ใช้ไฟล์ .htaccess แต่ถ้าเราใช้ Internet Information Services (IIS) ก็ต้องไปใช้ไฟล์ ที่คู่กันคือ Web.Config แทน

การที่จะ rewrite url ใน iis จะต้องมีการติดตั้ง URL Rewrite extension เอาไว้ก่อน

ตัวอย่างไฟล์ Web.Config ที่ทำหน้าที่ แปลง ค่าใน url ไปให้ไฟล์ index.php โดยที่จะซ่อนไฟล์ index.php ออกจาก url

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
   <system.webServer>
      <rewrite>
         <rules>
            <clear />
            <rule name="remove index.php" patternSyntax="Wildcard">
               <match url="*" />
               <conditions>
                  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
               </conditions>
               <action type="Rewrite" url="index.php" />
            </rule>
         </rules>
      </rewrite>
   </system.webServer>
</configuration>

นำไปว่างใน folder ที่ต้องการใช้เช่น folder public ของ laravel เป็นต้น

Byphunsanit

Bootstrap: dynamic tabs hash

เพราะว่า dynamic tabs ของ Bootstrap มันจะไม่เลือก tabs จาก hash หรือเครื่องหมาย # ใน url ถ้าต้องการให้แสดงแท็บที่ต้องการจาก links ต้องเขียน code เพิ่มเติมอีกเล็กน้อย

<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>Bootstrap: Dynamic Tabs With Hash</title>
<link href="../vendor/twbs/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>

<body>
<div class="container">
<h2>Dynamic Tabs With Hash</h2>

<input id="tabindex" name="tabindex" type="text">

<ul class="nav nav-tabs">

<li class="nav-item">
<a class="active nav-link show" data-toggle="tab" href="#menu1">Menu 1</a>
</li>

<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#menu2">Menu 2</a>
</li>

<li class="nav-item">
<a class="nav-link" data-toggle="tab" href="#menu3">Menu 3</a>
</li>
</ul>

<div class="tab-content">

<div id="menu1" class="active fade in show tab-pane">
<h3>Menu 1</h3>
<p>นาย</p>
</div>

<div id="menu2" class="fade tab-pane">
<h3>Menu 2</h3>
<p>พิชญ์</p>
</div>

<div id="menu3" class="fade tab-pane">
<h3>Menu 3</h3>
<p>พันธุ์สนิท</p>
</div>

</div>

</div>

<script src="../vendor/components/jquery/jquery.min.js"></script>
<script src="../vendor/twbs/bootstrap/dist/js/bootstrap.min.js"></script>
<script>
$(document).ready(function () {

let hash = window.location.hash.substr(1);
let tabindex = $('#tabindex');
if (hash != tabindex.val()) {
$('a[href="#' + hash).click();
tabindex.val(hash);
}

});
</script>

</body>

</html>

เมื่อเปิดหน้าเว็บมาจาก url ที่ใส่ # ของแท็บที่ต้องการมา เช่น http://localhost/snippets/Bootstrap/tabs.dynamic.hash.html#menu2 แท็บก็จะเปิดหน้าที่ถูกต้องตามที่ต้องการ แทนที่จะดูจาก class active เพียงอย่าเดียว

Byphunsanit

Yii2: pretty urls

โดยปกติ url ของ yii2 จะเป็นแบบ http://localhost/BanBan/1/backend/web/index.php?r=gii%2Fdefault%2Fview&id=model ซึ่งยากที่ user ทั่วไปจะจำ ทำให้ google มองว่าเว็บเราใช้งานยากจึงไม่เป็นผลดีในการทำ seo เราจึงต้องแปลง query string ให้เป็นรูปแบบ pretty urls ที่จะจำได้ง่ายและสวยงามกว่า พี่เกิ๊ลชอบ

  1. เปิดไฟล์ config\main.php จากนั้นเพิ่ม
        'components' => [
    ...
            'urlManager' => [
                'enablePrettyUrl' => true,
                'showScriptName' => false,
            ],
    ...
        ],
  2. จากนั้นสร้างไฟล์
    RewriteEngine on
    
    # if a directory or a file exists, use it directly
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    # otherwise forward it to index.php
    RewriteRule . index.php

    save ไปที่ backend\web และ frontend\web

เท่านี้จาก url ยาวๆ ก็จะสวยงามขึ้นกลายเป็น http://localhost/BanBan/1/backend/web/gii/model อ่านง่ายขึ้นเยอะเลย

Byphunsanit

curl: ส่ง ฟอร์มแบบ get

curl หรือ Client URL Library เป็น function ที่ทำให้ php ทำตัวเป็น browser ที่ใช้เปิดเว็บรับส่งข้อมูลต่างๆ จาก server ของเราไปเซิฟเวอร์เครื่องอื่นๆ

ตัวอย่างการใช้งานที่ง่ายที่สุด คือการใช้ curl โดยการจำลองการส่งข้อมูลจากฟอร์มแบบเมธอดเก็ต หรือที่เรียกง่ายๆว่า ส่งข้อมูลแบบ url นั่นละ

<!doctype html>
<html>

<head>
    <meta charset="utf-8">
    <title>CURL: send get variables</title>
    <link href="../vendor/twbs/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>

<body>
    <div class="container">
        <?php
if (count($_GET)) {
    $queryString = http_build_query($_GET);
    $url = 'http://localhost/snippets/PHP/variables.php?' . $queryString;

    echo '<br>$url = ', $url;

    $ch = curl_init();

    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_URL => $url,
    ]);

    $result = curl_exec($ch);
    curl_close($ch);

    echo $result;
}
?>
            <form action="curl_get.php" method="get">
                <div class="form-group">
                    <label for="name">Name:</label>
                    <input class="form-control" id="name" name="name" type="text">
                </div>
                <div class="form-group">
                    <label for="avatar">Avatar:</label>
                    <input accept="image/gif, image/jpeg, image/x-png" class="form-control" id="avatar" name="avatar" type="file">
                </div>
                <div class="form-group">
                    <label for="address1">text address:</label>
                    <input class="form-control" id="address1" name="address[]" type="text">
                </div>
                <div class="form-group">
                    <label for="address2">text address 2:</label>
                    <input class="form-control" id="address2" name="address[]" type="text">
                </div>
                <button type="submit" class="btn btn-default">Submit</button>
            </form>
    </div>
</body>

</html>

ในการแปลงข้อมูลจากแบบฟอร์มจะใช้

$queryString = http_build_query($_GET);

แปลงให้อยู่ในรูปแบบคิวรี่สตริงที่ส่งไปกับ url ได้

การทดสอบทำได้โดยไฟล์ variables.php จำลองเป็นฝั่งรับข้อมูล

<?php
echo '<h3>$_COOKIE</h3><pre>', print_r($_COOKIE, true), '</pre>';
echo '<h3>$_FILES</h3><pre>', print_r($_FILES, true), '</pre>';
echo '<h3>$_GET</h3><pre>', print_r($_GET, true), '</pre>';
echo '<h3>$_POST</h3><pre>', print_r($_POST, true), '</pre>';
echo '<h3>$_REQUEST</h3><pre>', print_r($_REQUEST, true), '</pre>';

ข้อมูลที่ส่งไปจะแสดงกลับมาให้เราเห็น แต่สังเกตุได้ว่า ถึงจะส่งไฟล์ avatar ฝั่งรับก็จะไม่ได้รับ เพราะการส่งข้อมูลแบบนี้จะมีข้อเสียคือ ไม่สามารถส่งไฟล์ได้