0

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!

5
  • 3
    Learn URL Rewritin with .htaccess Commented Dec 6, 2014 at 9:59
  • Eiter use .htaccess rules or if you are working with Wordpress you could dig into codex.wordpress.org/Rewrite_API, which is a wrapper for .htaccess Commented Dec 6, 2014 at 10:02
  • We do not use WordPress, We have our own CMS. Commented Dec 6, 2014 at 10:05
  • 1
    You should investigate how WordPress works it'll be useful for you. Commented Dec 6, 2014 at 10:13
  • I will Thanks for the Tip! Commented Dec 6, 2014 at 10:21

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah; have a Google for "PHP URL routing"; you'll find a bunch of decent explanations/tutorials on how a single entry point with the dispatching done in code works.
Is that .htaccess or PHP?
How can .htaccess help me? Because my URL's are dynamic all postID are Dynamic.
@mrezajafari gave you a link for FrontController pattern, read it - everything is explained there. Briefly, you redirect every request to index.php, which then parses $_SERVER["REQUEST_URI"] and calls needed function.
0

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.

1 Comment

I suggest start using laravel. it's easy to use and also easy to learn . laravel has so many vide tutorials and simple documantaion to learn. if you have'nt used any framework before , it's not wise to start writing your own framework.
0

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/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.