0

Is there a way to make this URL:

http://readme.com/read?blog=10092

look like this:

http://readme.com/read/blog/10092

Using PHP?

C_B
  • 2,620
  • 3
  • 23
  • 45
Nomad
  • 613
  • 2
  • 7
  • 15

4 Answers4

2

It is not possible via PHP itself. Even the above example (read?blog) is not a php-only solution.

PHP is a parsed file. The webserver parses a .php script and displays it to the viewer. So you have to configure your server (Apach for example) to use the correct php file for the request. The most common solution is mod_rewrite (http://httpd.apache.org/docs/current/mod/mod_rewrite.html an lots of tutorials). You will have to edit yout .httaccess file and create configuration options.

Michal
  • 733
  • 2
  • 6
  • 23
  • I have also encountered some URLs that look like this: http://readme.com/read/blog/10092.php how would I do this? – Nomad Jan 03 '14 at 10:03
  • That's a different aspect. Some search engines skip urls without an extention. In reality there is some blog.php and it recieves 10092.php as a paramerer (or it is pre-parsed by server and only 10092) so it works just like blog.php?id=10092. – Michal Jan 03 '14 at 10:05
1

One technique is by rewriting the url using .htaccess. Create a .htaccess (There's a dot in front of the file name!) file inside of your root folder and place the following code inside:

Options +FollowSymLinks
RewriteEngine on

RewriteRule /blog/(.*)/ read?blog=$1

Now you can go to the same page using the url :

http://readme.com/read/blog/10092
Hyder B.
  • 10,900
  • 5
  • 51
  • 60
  • What does [`Options +FollowSymLinks`](http://httpd.apache.org/docs/2.2/mod/core.html#options) have to do with this question? – eggyal Jan 03 '14 at 10:26
0

It looks like you need a PHP framework, like CakePHP or CodeIgniter. One of their main feature is the routine mechanism. However, you must first understand some Object-Oriented Programming in PHP.

Ray Yen
  • 55
  • 9
0

Its more complicated with plain php, you have to use htaccess too. See this example:

RewriteEngine on
RewriteRule ^/read/blog/([0-9]+)$ /read?blog=$1

Url rewriting is powered by .htaccess and inside it, regular expressions. If you want to know more about regular expressions, see wikipedia: https://en.wikipedia.org/wiki/Regular_expression

aksu
  • 5,221
  • 5
  • 24
  • 39
  • Okay so if I do this my URL should look like this: http://readme.com/read/blog/10092 but how would I get 10092 from the URL? A superglobal maybe? – Nomad Jan 03 '14 at 10:35
  • You can just access it like normal url parameter: `$blogID = $_GET['blog']` – aksu Jan 03 '14 at 10:37