Jump to content

How can I get/make the url in http://project.com/id/title format?


Recommended Posts

Normally I make url in this format:

http://project.com?id=10&title=abcd

Then get the value in this way:

$id=$_GET['id'];
$title=$_GET['title'];

But I see some websites with url in this format:

http://project.com/10/abcd

How can I make the url in this above format? How do those websites make their url in this format? After that how can I get the value of id and title with php?

 

It's called URL rewriting. It happens almost entirely at the web server level, where it will accept /10/abcd and internally rewrite it to index.php?id=10&title=abcd. That way your PHP code does not have to change.

 

Using Apache? Here are a few links to check out:

- http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

- https://httpd.apache.org/docs/trunk/rewrite/intro.html

- https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

 

(basically all I did was search for "apache url rewriting")

There are many different implementations, though. It can be as simple as one-to-one mapping:

/products/10 ----> products.php?id=10

or as complex as a router (like this one) which accepts all URLs, parses them and delegates the request to the actual location:

/products/10 ----                                  ----> products.php?id=10
                |                                  |
                ----> Router: index.php?url=... ---- 
                |                                  |
/users/42    ----                                  ----> users.php?id=42
  • 2 months later...

It's called URL rewriting. It happens almost entirely at the web server level, where it will accept /10/abcd and internally rewrite it to index.php?id=10&title=abcd. That way your PHP code does not have to change.

 

Using Apache? Here are a few links to check out:

- http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

- https://httpd.apache.org/docs/trunk/rewrite/intro.html

- https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

 

(basically all I did was search for "apache url rewriting")

When we use a php framework, we don't change the webserver to make such urls. The php framework automatically changes that index.php?id=5 to index/5. How do they do it internally?

By using URL rewriting. Some amount of it must happen: individual rules will give better performance because Apache can do it faster than you can, however it's also possible to route all non-existent requests through PHP and do the "rewriting" in code.

 

Frameworks typically do the latter because they're larger and generally have a single point of entry for all requests anyways.

When we use a php framework, we don't change the webserver to make such urls. The php framework automatically changes that index.php?id=5 to index/5. How do they do it internally?

Most sites DO use server based url rewriting, since it's faster and more transparent than having to do parameter extraction in php.

 

With that said, PHP makes the full URL available in various CGI variables. For example, the portion without the scheme and host is made available in:

 

echo $_SERVER["REQUEST_URI"];
This does require that the site is configured to default to running a script (typically index.php) when no target exists.

 

At that point, it is simply a matter of breaking up the URI into pieces and assigning them to the same variables you would otherwise have provided. PHP has useful string handling functions like explode that can break a url into pieces easily.

 

What is important is that there be a mapping between a specific url pattern and the routing of that url to a controller with parameters passed as variables.

 

So it's really valuable to look at routing libraries. For example, the slim framework uses this component library to provide routing services: https://github.com/nikic/FastRoute

 

Symfony has the routing component: http://symfony.com/doc/current/components/routing.html

 

Taking a look at the code in these libraries should help you answer your question as to what type of code is needed.

When we use a php framework, we don't change the webserver to make such urls. The php framework automatically changes that index.php?id=5 to index/5. How do they do it internally?

Are you certain? If the url's being rewritten, *someone* changed the server's behavior. Does the framework ship with an ".htaccess" file, for example?

Edited by dalecosp

I've started to play with Laravel and there is a .htaccess in the 'root' (actually called public) directory of the application.  This does exactly as your asking about and changes the URL into a mangled index.php call.

 

Have a look at https://laravel.com/docs/5.0/configuration#pretty-urls to see how it's achieved.

But when we use any php framework we don't configure or change anything in apache. But the url looks like http://myproject.com/10/abcd . How do php frameowrks do that?

 

https://laravel.com/docs/5.0/configuration#pretty-urls ,  Also I copied these in .htaccess and put this .htaccess in myproject/ root directory, rewrite module is enabled, then tried http://localhost/myproject/list/a , but it doesn't work.

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

apache2.conf =>

<Directory /var/www/>
        Options Indexes FollowSymLinks
        Options +SymLinksIfOwnerMatch
        AllowOverride All
        Require all granted
</Directory>

http://localhost/myproject/list.php?id=a works.

Edited by colap

It's called URL rewriting. It happens almost entirely at the web server level, where it will accept /10/abcd and internally rewrite it to index.php?id=10&title=abcd. That way your PHP code does not have to change.

 

Using Apache? Here are a few links to check out:

- http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

- https://httpd.apache.org/docs/trunk/rewrite/intro.html

- https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/

 

(basically all I did was search for "apache url rewriting")

How would php website know /10/abcd is index.php?id=10&title=abcd ? How would php know if the parameters are id and title or something else?

How about reading the damn replies?

 

We've told you at least three times that frameworks use routers. gizmola has posted a link to such a router, so you can actually see how it works.

 

How does PHP know how to rewrite the URLs? It doesn't rewrite them. There's a simple rule in the webserver configuration which maps all requests to the router, and then the router delegates the request to the right function or controller based on the URL.

1) Can we use any router library to avoid apache configuration? I don't want to touch apache configuration files.

 

2) If I must edit apache configuration, what are those rules to edit?

 

3) Or can we just edit apache configuration and avoid using php router library(vice-versa)?

 

4) Or should we need to use a php router library and edit apache configuration file(both needed)?

Have a read of https://www.digitalocean.com/community/tutorials/how-to-rewrite-urls-with-mod_rewrite-for-apache-on-ubuntu-16-04 which shows how if you just use Apache rewrite rules, whats involved.  This is why frameworks are used to decode what your trying to do.

1. No. There must be something telling Apache to use PHP for URIs that it would not normally do so. But if your problem is messing with "Apache configuration files" then you can use .htaccess instead - in fact that's what most people do.

2. We've given you many links and even a few samples to try out.

3. Probably? Depends on your code. You can certainly rewriting particular URI formats to particular files without needing a whole "router library". As for vice-versa, see #1.

4. A router library may be more than you need. Again, depends on your code, again, you can disregard such libraries and only rewrite directly to individual scripts.

 

Many of these sorts of questions are already answered with general information about URL rewriting. Please read those links so we don't have to repeat what they say.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.