PHP Dynamic Db Page Rewrite Url

PHP dynamic DB page rewrite URL

This is known as a "slug" wordpress made this term popular. Anyway though.

Ultimately what you need to do is have an .htaccess file that catches all your incoming traffic then reforms it at the server level to work with your PHP in the sense, you will still keep the ?id=123 logic intact, but to the client side '/folder/FHJKD/' will be the viewable result.

here is an example of an .htaccess file I use a similar logic on.. (so does wordpress for that matter).

RewriteEngine On
#strips the www out of the domain if there
RewriteCond %{HTTP_HOST} ^www\.domain\.com$

#applies logic that changes the domain from http://mydomain.com/post/my-article
#to resemble http://mydomain.com/?id=post/my-article
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?id=$1 [QSA,L]

what this will do is take everything after domain.com/ and pass it as a variable to index.php the variable in this example would be 'id' from this you have to device a logic that best suits your sites needs.

example

<?php
//the URL for the example here: http://mydomain.com/?id=post/my-article
if($_GET['id'])
{
$myParams = explode('/', $_GET['id']);
echo '<pre>';
print_r($myParams);
echo '</pre>';
}
?>

now the logic for this would have to go much deeper, this is only pure example at a basic level, but overall and especially cause your working with a database I assume, your gonna wanna make sure the $myParams is clean of malicious code, that can inject into your PHP or Database.

The output of the above $myParams via print_r() would be:

Array(
[0] => post
[1] => my-article
)

To work with it you would need to do at the very least

echo $myParams[0].'<br />';

or you could do it like this cause most browsers will add a final /

<?php
//the URL for the example here: http://mydomain.com/?id=post/my-article
if($_GET['id'])
{
//breaks the variable apart, removes any empty array values and reorders the index
$myParams = array_values(array_filter(explode('/', $_GET['id'])));
if(count($myParams > 1)
{
$sql = "SELECT * FROM post_table WHERE slug = '".mysql_real_escape_string($myParams[1])."'";
$result = mysql_query($sql);
}

}
?>

Now this admitedly is a very crude example, you would want to work some logic in there to prevent mysql injection, and then you will apply the query like you would how you are now in pulling your articles out using just id=123.

Alternatively you could also go a completely different route, and explore the wonders of MVC (Model View Control). Something like CodeIgniter is a nice easy MVC framework to get started on. But thats up to you.

rewrite-dynamic-url using php and .htaccess

Add following code in .htaccess file

RewriteEngine on 
Options +FollowSymlinks
RewriteBase /
RewriteRule ^product\.php$ - [L]
RewriteRule ^(.*) /product.php?product=$1 [L]

And following in product.php for example

<?php

print_r($_GET);

Now once you visit domain.com/test-product.html OR domain.com/test-product-1.html OR domain.com/test-product-2.html etc.

you will get output like:

Array ( [product] => test-product.html ) 

Now you can use this product variable and query database to dynamically generate page.

It seems like I am doing coding for you :)

Check this good reference: http://www.sitepoint.com/guide-url-rewriting/

change php dynamic pages url

To accomplish this, you need to first create a text document called ".htaccess" to contain your rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). There may already be a .htaccess file there, in which case you should edit that rather than overwriting it.

RewriteEngine On 
RewriteRule ^post/([0-9]+)/?$ ?post=$1 [NC,L]

NOTE: Only If Application runs on Apache

How to change my URL structure on dynamic selection ID which is retrieving from database

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:

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.

php dynamic table page content based on url

If you’re using Apache, put this into .htaccess file in your folder:

RewriteEngine on
RewriteRule "^([a-zA-Z0-9]+)$" "index.php"

https://httpd.apache.org/docs/2.4/rewrite/intro.html

Load Database Data As dynamic Page

You probably want to first tell .htaccess to process all missing pages with some php script you've written, by putting this inside your .htaccess file:

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /my_processor.php [L]
</IfModule>

... which would effectively tell apache that all requests to pages that don't exist, should be redirected to a script called my_processor.php, directly on your website's document root.

Once inside the my_processor.php script, you can look at and parse $_SERVER['REQUEST_URI'], and use that to determine what your script should do next. ie:

[my_processor.php]
<?php

//
@$uri=$_SERVER['REQUEST_URI'];

switch($uri)
{

default: die("Error");

case "/some-path-1/":
SomeFunction1();
break;

case "/some-path-2/":
SomeFunction(2);
break;
}

?>

Please forgive how simple the above is, but hopefully you get the idea.

PHP simple dynamic URL routing

Try removing the query from the request uri.

And you can't stick query parameters in the path to an included file. But you can populate the get parameter if you want the included script to behave like it was submitted. But from the looks of it I think you should avoid that.

$request = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

switch ($request) {

case '/':
require __DIR__ . '/views/index.php';
break;

case '/link-one':
$_GET['page_slug'] = 'link-one';
require __DIR__ . '/views/page.php';
break;

}

And maybe this .htaccess is better in the long run where query string is appended to the route. And this is the last rule not conflicting others that may follow:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]


Related Topics



Leave a reply



Submit