• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Redirect all urls to lowercase

#1
My question is how can i set header 301 for this redirect ?
I think i didn't setup correctly header 301 for this redirect.

PHP Code:
$response->setHeader('HTTP/1.1 301 Moved Permanently''');
$response->redirect($uri);
$response->sendHeaders(); 

Recently i created this code for redirect all urls from my app to lowercase. If someone have a good idea please tell me.

App/Controllers/BaseController.php

PHP Code:
<?php namespace App\Controllers;
use 
CodeIgniter\Controller;

class 
BaseController extends Controller {

    protected 
$helpers = [];

    public function 
initController(\CodeIgniter\HTTP\RequestInterface $request, \CodeIgniter\HTTP\ResponseInterface $response, \Psr\Log\LoggerInterface $logger) {
        
        
parent::initController($request$response$logger);
    
        
forceLowercase($request$response);

    }
    


app/Common.php

PHP Code:
<?php
 
use CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\HTTP\URI;
use 
Config\Services;

if ( !
function_exists('forceLowercase') ) {

    function 
forceLowercase(RequestInterface $request nullResponseInterface $response null) {
        
        if ( 
is_null($request) ) {
            
$request Services::request(nulltrue);
        }
        
        if ( 
is_null($response) ) {
            
$response Services::response(nulltrue);
        }
        
        if ( 
strtolower((string) $request->uri->getPath()) != (string) $request->uri->getPath() ) {
            
            
$baseURL config(App::class)->baseURL;

            if (
strpos($baseURL'http://') === 0) {
                
$baseURL = (string) substr($baseURLstrlen('http://'));
            }

            
$uri URI::createURIString(
                
'https'$baseURLstrtolower($request->uri->getPath()),
                
$request->uri->getQuery(), $request->uri->getFragment()
            );
             
            
$response->setHeader('HTTP/1.1 301 Moved Permanently''');
            
$response->redirect($uri);
            
$response->sendHeaders();
            
            exit;
            
        }
        
    }
    

Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.