Can anyone explain how to create friendly URLs? I mean URLs like http://store.steampowered.com/app/22600/ that doesn't have any pages like index.php visible.
If you only have cpanel use .htaccess.
If that doesn't work, you are left with parsing the url in php with a link like this:
http://server.com/router.php/search
You can do that with something like this.
<?
list($junk,$url) = explode("router.php",$_SERVER['REQUEST_URI']);
$paths = explode("/", $url);
if ($paths[0] == 'search')
{
Header("Location: /search.php");
}
?>
http://articles.sitepoint.com/article/search-engine-friendly-urls
http://www.alistapart.com/articles/succeed/
Search revealed dozens more
You need to look up apache mod_rewrite (assuming you are using apache for your web server). PHP itself doesn't do it for you the web server does most of the work. You need to tell your web server to use mod_rewrite to point all urls that match a certain pattern to point to what ever .php file you like. You can pass arguments in your url pattern what ever way you choose. e.g. using the / to delimit key values or just values. If you don't have root access to the server and its enabled you can usually put these mod rewrite rules in a .htaccess file at the root of your site.
Sitepoint have a good reference for beginners.
http://articles.sitepoint.com/article/guide-url-rewriting
if you're using apache this should work/is the idea:
RewriteRule ^(.*)$ /index.php?/$1 [L]
now your url will go to http://store.steampowered.com/index.php/app/22600/
Related
I am trying to find a way to make pages accessible by pure URL using the $_GET[''].
But instead of the following request URL :
http://mywebsite.com/product.php?=1
I want :
http://mywebsite.com/product/1
Thank you and have a nice day.
You will need an PHP - URL-Router and mod_rewrite enabled on your Webserver.
If you're not that experienced you can use a PHP Framework, many Frameworks do have a Good Routing integrated.
Have a Look at some example tutroium: http://blogs.shephertz.com/2014/05/21/how-to-implement-url-routing-in-php/
You Should using Friendly url in php for creating spec url
you can use this approach:
how-to-create-dynamic-friendly-urls-using-php
how-to-create-friendly-url-in-php
please search friendly url in php
Create a htaccess file at root and write this code in that
RewriteEngine On
RewriteRule ^/product.php?id=([0-9]+)$ /product/$1
For more detail read about htaccess
If you are checking it at localhost than make sure you have restart your server after writing this code
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)$ index.php?$1=$2
The more number of arguments you enter here that you can access through url
$1=$2
$1=$2&$3=$4
$1=$2&$3=$4&$4=$5
Your answer is given here.
This can be achieved via url rewriting. Sounds like you want to achieve something like what a lot of PHP frameworks do.
for example http://codeigniter.com/user_guide/general/urls.html
When you edit a question on stackoverflow.com, you will be redirected to a URL like this:
https://stackoverflow.com/posts/1807421/edit
But usually, it should be
https://stackoverflow.com/posts/1807491/edit.php
or
https://stackoverflow.com/posts/edit.php?id=1807491
How was
https://stackoverflow.com/posts/1807421/edit
created?
I know that Stackoverflow.com was not created by using PHP, but I am wondering how to achieve this in PHP?
With apache and PHP, you might perform one of your examples using a mod_rewrite rule in your apache config as follows:
RewriteEngine On
RewriteRule ^/posts/(\d+)/edit /posts/edit.php?id=$1
This looks for URLs of the "clean" form, and then rewrites them so that they are internally redirected to a particular PHP script.
Quite often rules like this are used to route all requests into a common controller script, which might do something like instantiate a "PostsController" class and ask it to handle an edit request. This is a common feature of most PHP application frameworks.
It's called routing. Take a look at tutorials on the subject.
If you use a framework such as cake php it should be built in.
As #mr-euro stated you can use mod_rewrite but front controller is a far better solution.
You force every request to index.php and you write your application controlling in index.php.
You use Apache's .htaccess/mod_rewrite, and optionally a PHP file, which is the approach I like to take myself.
For the .htaccess, something like this:
RewriteEngine On
RewriteRule ^(.*)$ index.php
Then in your PHP file, you can do something like this:
The following should get everything after the first slash.
$url = $_SERVER['REQUEST_URI'];
You can then use explode to turn it into an array.
$split = explode('/', $url);
Now you can use the array to determine what to load:
if ($split[1] == 'home')
{
// display homepage
}
The array is starting from 1 since 0 will usually be empty.
It's indeed done by mod_rewrite, or with multiviews. But i prefer mod_rewrite.
First: you create a .htaccessfile with these contents:
RewriteEngine On
RewriteRule ^posts/([0-9])/(edit|delete)$ /index.php?page=posts&postId=$1&action=$2
Obvious, mod_rewrite must be enabled by your hostingprovider ;)
Using mod_rewrite this can be achieved very easily.
I am poor at this but i do know you can redirect urls using apache mod_rewrite and by touching config files. From what i remember htaccess can be used to redirect. Then internally when the user hits
http://stackoverflow.com/posts/1807421/edit it can use your page http://stackoverflow.com/edit.php?p=1807421 instead or whatever you want.
You could use htaccess + write an URI parser class.
I am seeing this url format at most websites.
site.com/extension/rar
I wonder how they get the value='rar' using $_GET.
What I know is that $_GET can be use in here
site.com/extension/index.php?ext=rar
Now I wanted to change my way of calling a variable.
I wanted to apply what most websites do.
How can I call variable in the former?
Perhaps this works to get the "rar":
$name = basename($_SERVER['REQUEST_URI']);
I most likely being done using .htaccess
It is an Apache module that allows you "rewrite" urls at the engine level based on your own set of rules. So basically it rewrites URLs on the fly.
So, in your example you could have a file named .htaccess with the following contents: (there may be other options)
RewriteEngine On
RewriteRule ^extension/([a-z0-9]+)$ somefile.php?extension=$1 [L]
Basically, you are saying: If someone is looking for a URL that looks like "extension/somenumbers-and-letters" then show the contents of "somefile.php?extension=whatever-those-number-and-leters-are".
Do a search on Apache mod_rewrite to find more information.
When you edit a question on stackoverflow.com, you will be redirected to a URL like this:
https://stackoverflow.com/posts/1807421/edit
But usually, it should be
https://stackoverflow.com/posts/1807491/edit.php
or
https://stackoverflow.com/posts/edit.php?id=1807491
How was
https://stackoverflow.com/posts/1807421/edit
created?
I know that Stackoverflow.com was not created by using PHP, but I am wondering how to achieve this in PHP?
With apache and PHP, you might perform one of your examples using a mod_rewrite rule in your apache config as follows:
RewriteEngine On
RewriteRule ^/posts/(\d+)/edit /posts/edit.php?id=$1
This looks for URLs of the "clean" form, and then rewrites them so that they are internally redirected to a particular PHP script.
Quite often rules like this are used to route all requests into a common controller script, which might do something like instantiate a "PostsController" class and ask it to handle an edit request. This is a common feature of most PHP application frameworks.
It's called routing. Take a look at tutorials on the subject.
If you use a framework such as cake php it should be built in.
As #mr-euro stated you can use mod_rewrite but front controller is a far better solution.
You force every request to index.php and you write your application controlling in index.php.
You use Apache's .htaccess/mod_rewrite, and optionally a PHP file, which is the approach I like to take myself.
For the .htaccess, something like this:
RewriteEngine On
RewriteRule ^(.*)$ index.php
Then in your PHP file, you can do something like this:
The following should get everything after the first slash.
$url = $_SERVER['REQUEST_URI'];
You can then use explode to turn it into an array.
$split = explode('/', $url);
Now you can use the array to determine what to load:
if ($split[1] == 'home')
{
// display homepage
}
The array is starting from 1 since 0 will usually be empty.
It's indeed done by mod_rewrite, or with multiviews. But i prefer mod_rewrite.
First: you create a .htaccessfile with these contents:
RewriteEngine On
RewriteRule ^posts/([0-9])/(edit|delete)$ /index.php?page=posts&postId=$1&action=$2
Obvious, mod_rewrite must be enabled by your hostingprovider ;)
Using mod_rewrite this can be achieved very easily.
I am poor at this but i do know you can redirect urls using apache mod_rewrite and by touching config files. From what i remember htaccess can be used to redirect. Then internally when the user hits
http://stackoverflow.com/posts/1807421/edit it can use your page http://stackoverflow.com/edit.php?p=1807421 instead or whatever you want.
You could use htaccess + write an URI parser class.
lets say i want to go to
http://example.com/something/a/few/directories/deep
is there a way I can process this in PHP without having to make those directories? I just want to get everything after the domain name as a variable and work from there.
The most common way of doing this is by using mod_rewrite. (There in an introduction to this at http://articles.sitepoint.com/article/guide-url-rewriting). This allows you to internally map these friendly URLs to query parameters passed to your PHP script.
Sure. You don't even need mod_rewrite if the beginning is constant or has only a few possible values. Make a php file named something (no extension) and tell Apache (via a SetHandler or similar directive) to treat it as PHP. Then, examine $_SERVER['PATH_INFO'] in the script, which will look like a/few/directories/deep IIRC. You can use that to route your request however you like.
Alternatively. as Martin says, use a simple mod_rewrite rule to rewrite it to /dispatch.php/something/a/few/directories/deep and then you have the whole path in PATH_INFO.
You do not need mod_rewrite to handle such a scenario. Most of the popular PHP frameworks currently support URI segment parsing. This is a simple example which still leaves a few security holes to be patched up.
You could handle this by taking one of the global $_SERVER variables and splitting it on forward slashes like so:
if (!empty($_SERVER['REQUEST_URI'])) {
$segments = explode('/', $_SERVER['REQUEST_URI']);
}
The $segments variable will now contain an array of segments to iterate over.
Assuming you are using apache...
Add a re-write rule (in your vhost conf or just drop .htaccess file in you doc root), to point everything to index.php. Then inside of your index.php, parse the request uri and extract path
RewriteEngine on
RewriteRule ^.*$ index.php [L,QSA]
index.php:
$filepath = $_SERVER['REQUEST_URI'];
$filepath = ltrim($myname, '/'); //strip leading slash if you want.
This actually is not a PHP5-related quesion - URL rewriting is part of your webserver.
You can, when you use mod_rewrite on Apache (don't know about IIS, but there likely is something similar).