I have a dynamic URL like http://example.comjobdetail.php?id=24 and I want to change this URL like http://example.comjobdetail/24 how can we do this ?
Asked
Active
Viewed 75 times
0
-
There are many ways, all depending on how your application is structured and run. As it is, your question is way too broad to give you a significative answer. – Federico klez Culloca Mar 06 '18 at 08:11
-
2This is usually done using a rewrite in the webserver, not in PHP. – Barmar Mar 06 '18 at 08:11
-
Are you doing this in native PHP (no framework?)? What you are searching is url rewriting (and may require database manipulation, .htaccess). For example https://openclassrooms.com/courses/du-rewriting-realise-avec-du-php If you are working on a symfony (or many other framework), thnigs can be way simpler. – nicolallias Mar 06 '18 at 08:12
-
@barmar, what about [slim](https://www.slimframework.com/)? – Federico klez Culloca Mar 06 '18 at 08:12
-
What web server are you using? A very common method is using .htaccess and `RewriteRule`s – LeonardChallis Mar 06 '18 at 08:13
-
@nicolallias a link in French? – Federico klez Culloca Mar 06 '18 at 08:13
-
Yeah, I though that was less rude that "let me google that for you" :) – nicolallias Mar 06 '18 at 08:14
-
1Possible duplicate of [How to make Clean URLs](https://stackoverflow.com/questions/34048235/how-to-make-clean-urls) – aidinMC Mar 06 '18 at 08:14
-
I'm using apache web server and the platform is core PHP and using .htaccess – Mar 06 '18 at 08:38
2 Answers
0
If you are using Apache web server, you can use .htaccess file for this. Example:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^jobdetail\.php$ /jobdetail/%1? [L]
Also pls look to the related Question: .htaccess rewrite "/book.php?id=1234" to "/book/1234"

Sergei Shitikov
- 287
- 2
- 12
0
Original URL:
http://www.example.com/jobdetail.php?id=24
Desired destination URL:
http://www.example.com/jobdetail/24/
.htaccess syntax:
RewriteEngine On
RewriteRule ^/?jobdetail/([^/d]+)/?$ jobdetail.php?id=$1 [L,QSA]
EDIT
If CSS/ JS don't load, add this line to your jobdetail.php header
<base href="http://example.com/">

Diogo Jesus
- 318
- 4
- 19