1

I am building a router in PHP. I was wondering how I can map /profile?id=3 to /user/3. Is there any way to do this in PHP without .htaccess?

tereško
  • 58,060
  • 25
  • 98
  • 150
user3660432
  • 33
  • 1
  • 4
  • You need htaccess to rewrite the url. you cannot do it otherway. – machineaddict May 21 '14 at 10:50
  • Yes put the rewrite rules in the vhost config instead – PeeHaa May 21 '14 at 10:51
  • Your sure of it? I am using the MVC pattern and redirecting all requests to index.php and requiring the router with all query strings after it. I want to make this as easy as possible for a user to rewrite the URL. @machineaddict – user3660432 May 21 '14 at 10:52
  • 1
    @PeeHaa in the vhost? Could you give an example? – user3660432 May 21 '14 at 10:53
  • @PeeHaa: the OP was made probably because the user doesn't have access to create a htacess file – machineaddict May 21 '14 at 10:56
  • @user3660432: and how are you doing that? – machineaddict May 21 '14 at 10:56
  • I didn't, but I was just wondering so it could be simpiler in the future. @PeeHaa – user3660432 May 21 '14 at 10:56
  • It works in the same way. Only difference is that you do the config where it belonsg (in the server config) instead .htaccess files changes can be disabled on certain hosts. – PeeHaa May 21 '14 at 10:57
  • Dear OP, this has nothing to do with MVC. Tag has been removed. Also, it is possible to do `i.php/something/something`. In the `i.php` file the `'/something/something'` part will end up in one of `$_SERVER` params. – tereško May 21 '14 at 11:24

2 Answers2

0

The URL needs to be rewritten from that shortened form, to a query string to be passed to PHP. You cannot accept /user/3 in PHP in any way as it's not going to be populated in the $_GET superglobal array.

You don't need to put rewrite rules in .htaccess files, as they're per-directory inherited. But you still need mod_rewrite.

As you're using PHP, I'm assuming you're not running it on the CLI, so you must be using Apache or Nginx. That being the case, what's the problem running mod_rewrite and putting the rules in the config?

i-CONICA
  • 2,361
  • 9
  • 30
  • 45
  • 1
    There is no problem, I just want it simple to rewrite a URL for the future, instead of having to come up with a rewrite rule. – user3660432 May 21 '14 at 10:55
  • What you could do is create a base entry point which catch all urls like in [Symfony](http://symfony.com/en/doc/current/book/routing.html), for example. You'll only have to put one rule in your apache config ;) – Agate May 21 '14 at 11:06
  • Ok, I think I see now. I would create an instance of the router, and want to pass through the page with the placeholder, and the say ID. Then I would show the page? @EliotBerriot – user3660432 May 21 '14 at 11:10
  • @user3660432, please see my anwser for details (comments are too short for this) – Agate May 21 '14 at 11:27
0

What you could do is create a base entry point which catch all urls like in Symfony, for example.

This entry point could be a app.php file at the root of your project. You can access it via urls like http://yourdomain.com/app.php/user/3.

This entry point will use the URL part after app.php to invoque corresponding controller, extract the user ID and render HTML.

Then, in your apache config, you create a rewrite rule that will prepend all URLs with app.php. http://yourdomain.com/user/3 will be mapped under the hood by apache to http://yourdomain.com/app.php/user/3.

An exemple of such a rewrite rule can be found here.

After that, if you want to support other routes, like /myblog/great-entry or anything else, you can handle them from PHP side. You won't have to edit your apache config ever again, because every URL are catched by your app.php file.

Community
  • 1
  • 1
Agate
  • 3,152
  • 1
  • 19
  • 30