Search
 
SCRIPT & CODE EXAMPLE
 

PHP

url rewrite htaccess php

You can essentially do this 2 ways:

The .htaccess route with mod_rewrite
Add a file called .htaccess in your root folder,and add something like this:

RewriteEngine on
RewriteRule ^/?Some-text-goes-here/([0-9]+)$ /picture.php?id=$1
This will tell Apache to enable mod_rewrite for this folder,
and if it gets asked a URL matching the regular expression it rewrites it
internally to what you want, without the end user seeing it.
Easy, but inflexible, so if you need more power:

The PHP route
Put the following in your .htaccess instead: (note the leading slash)

FallbackResource /index.php
This will tell it to run your index.php for all files it cannot normally find in your site. In there you can then for example:

$path = ltrim($_SERVER['REQUEST_URI'], '/');    // Trim leading slash(es)
$elements = explode('/', $path);                // Split path on slashes
if(empty($elements[0])) {                       // No path elements means home
    ShowHomepage();
} else switch(array_shift($elements))             // Pop off first item and switch
{
    case 'Some-text-goes-here':
        ShowPicture($elements); // passes rest of parameters to internal function
        break;
    case 'more':
        ...
    default:
        header('HTTP/1.1 404 Not Found');
        Show404Error();
}

This is how big sites and CMS-systems do it, 
because it allows far more flexibility in parsing URLs, 
config and database dependent URLs etc.
For sporadic usage the hardcoded rewrite rules in .htaccess will do fine though.
Comment

PREVIOUS NEXT
Code Example
Php :: php Convert multidimensional array into single array 
Php :: curl failed laravel centos 
Php :: how to add image in wordpress theme 
Php :: twig render to string 
Php :: php find in array 
Php :: laravel check model column was changed 
Php :: php find all subclasses of class 
Php :: extend multiple classes in php 
Php :: php two array difference merge recursive 
Php :: @yield extends laravel 
Php :: php sms sending script 
Php :: meta_value wordpress 
Php :: phpmailer send email to multiple addresses 
Php :: laravel follow and unfollow relationship 
Php :: check dir php 
Php :: laravel link to css or image 
Php :: add column migration laravel 8 
Php :: php in html need .htaccess 
Php :: mail function php not working 
Php :: view blade not found in laravel 
Php :: php $this 
Php :: template engine php 
Php :: php class extends two classes 
Php :: merge pdf php fpdf 
Php :: Script @php artisan package:discover handling the post-autoload-dump event returned with error code 255 
Php :: how to develop package beside laravel project 
Php :: split functions.php 
Php :: auto complete order 
Php :: Export database to sql dump in php 
Php :: yii2 rollback last migration 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =