Get information from custom url

2019-05-26 22:30发布

问题:

I'm working on a website for a school project, but i've run into a little problem. I want to use my url to "store information". I'm using HTML5 + PHP + JavaScript. Normally i would do it like this: http://www.example.com/index.php?files=share, but i want it to be like this: http://www.example.com/files/share. I don't know if it is possible with any of the languages i use, but if there is a way with an other language, i'm happy to know!

MB

回答1:

The most plain and simple way to do it with the technologies you are using is to use .htaccess and mod_rewrite to format your urls and make them "pretty":

http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

However there are a number of frameworks with routing setups that make it a little more automatic, but they have their own learning curve. I am not sure if since this is a school project that would be considered cheating, however if that is not an issue you could checkout a php framework like laravel, codeignitor, yii etc etc etc.

It would probably be good, for the sake of learning, to experiment with the .htaccess solution.



回答2:

This can be achieved by using a rewrite (mod_rewrite on Apache) to force all requests through a single PHP file - say index.php. This StackOverflow question covers rewriting well:

Redirect all to index.php htaccess

You can subsequently parse $_SERVER['REQUEST_URI'] to handle routing of requests to the correct place and the extraction of parameters from the URL.

Note that this is the standard behaviour of most MVC frameworks and, where possible, you should avoid reinventing the wheel and look in their direction for your implementation.