I want to know if we can achieve something like this:
example.com/apps?id='blahblah'
converted to example.com/apps/id/blahblah
ie. parameters becoming pages themselves. I want to create seperate urls for each id but dynamically. Is this possible in php or any other workaround.
Asked
Active
Viewed 513 times
0

clint
- 1,786
- 4
- 34
- 60
-
What you are looking for is called [mod_rewrite](https://httpd.apache.org/docs/2.4/rewrite/) – Mihai Matei May 03 '16 at 12:18
-
Can't understand why people have downvoted. Is it necessary that i will know about mod_rewrite. It is knowledge based and not IQ based. – clint May 03 '16 at 12:31
2 Answers
1
You need to use .htaccess to achieve this.
.htaccess basically rewrites the url to your original location. So to do what you asked in your question it would be like this:
RewriteEngine on
RewriteRule apps/id/(.*?)$ apps?id=$1

Ghulam Ali
- 1,935
- 14
- 15
-
i have put the same and trying with localhost/xyz/apps/id/123, but it does not redirect to localhost/xyz/apps?id=123. Can you point out my mistake. – clint May 03 '16 at 13:27
-
It will not redirect, it will rewrite (show same page in different url). Secondly, You are using xyz sub directory. So have you placed this .htaccess in the xyz directory? – Ghulam Ali May 03 '16 at 13:29
-
yes i have placed this in xyz directory. so according to you, if we type localhost/xyz/apps/id/123, it will rewrite to localhost/xyz/apps?id=123 and hit the index.php file in apps directory, right? But that is not happening. – clint May 04 '16 at 05:14
-
It will not hit index.php, it will goto 'apps'. If you want index.php then use index.php?id=$1 (Also before rewriting first open the original url in your browser to verify it is working. In this case http://localhost/xyz/apps?id=123) – Ghulam Ali May 04 '16 at 07:56
1
You're probably getting downvoted because "mod rewrite in php" has been widely documented with tutorials and articles. Try to search for it here or on Google and you will find plenty of results.
Anyway, if you can't or won't use Apache's mod_rewrite
, you can do the same in pure PHP by parsing the HTTP request and process accordingly.
For example
$request_path = $_SERVER['PATH_INFO'];
$request_path = explode("/", $request_path);
// example.com/apps.php/id/blahblah
// $request_path[0] => "example.com"
// $request_path[1] => "apps.php"
// $request_path[2] => "id"
// $request_path[3] => "blahblah"
Notice I added .php
extension to apps
. If you wish to hide the .php
extension you must use mod_rewrite
anyway.
From here on you can use the $request_path
to figure out what the request wants.

KEK
- 473
- 3
- 15