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
Related
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.
I want to add multiple languages to my website in the way that I would like something like http://www.example.com/EN/login for English and http://www.example.com/DE/anmelden for German. Both of the URLs should pass the requests to http://www.example.com/login.php.
Now I am wondering in general how would you create a URL like that, do I need a file anmelden.php inside the DE directory? Or does the directory DE/anmelden not exist and its all done with .htaccess? And how can I extract the "DE" and "anmelden"?
Well, since you are using PHP, you will want to use a .htaccess file to do your URL rewriting. Check out this beginner's guide to mod_rewrite.
This is the example they use, which rewrites from index.php?page=software to /page/software:
RewriteEngine on
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
The EN/DE can be achieved through htaccess and passed to a script. You can extract the URL by using $_SERVER['REQUEST_URL']; from that script and output/forward the proper view
http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/
This will help you allot not in languages but in other functionality too
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/
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.