Reputation: 483
Is there any way I can have good looking URL in PHP? any default php URL would look like this: http://example.com/something/?post=something But Is It possible to have It like this: http://example.com/something/user Is It possible to remove ?post= without using .htaccess
Here is some example code that I have been working on, Which on click of a post It would access my database and load the content:
<?php
if(!isset($_GET['post'])) {
$q = mysql_query("SELECT * FROM posts WHERE postID='something'");
} else {
$id = $_GET['post'];
$id = mysql_real_escape_string($id);
$q = mysql_query("SELECT * FROM posts WHERE postID='$id'");
}
$p = mysql_fetch_object($q);
?>
Thank you for your Time!
Upvotes: 0
Views: 788
Reputation: 108
You need to define a Router and Dispatcher in your project.The Router extracts url and dispatcher calls that function related to url.in the other word,you should impelement frontcontroller design pattern in your project.I suggest check this tutorial http://www.sitepoint.com/front-controller-pattern-1/
Upvotes: 0
Reputation: 2301
I suggest for having clean URL use one of php frameworks which default have this ability. if you want to use pure php you should use a Router in your project which means you should write your own php framework. have you ever worked with any php framework?? I suggest using laravel or cakephp for entry point of learning php frameworks.
Upvotes: 0
Reputation: 157
To get clean URLs you'll have to use mod_rewrite module, but you can minimize it's use, if you leave url parsing to your own script and have only one entry point. Look how it's made in WordPress:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
So if there's no real file or directory that is requested in URL, each and every request is redirected to index.php
and then parsed by some internal route mapping script.
There's an example of such script in similar question.
Upvotes: 1