简体   繁体   中英

php - clean URL

I want to create a web site with pure PHP. I want to hide the url parameters. Ie I want to make my web site with clean urls. Is there is any way to do this with out using any framework? Is cURL helpful to do this?

See URL rewriting in PHP without .htaccess if you don't want to or can't use .htaccess , else refer to How to: URL rewriting in PHP? .

只是看看...开始之前

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

First of all: It is not possible with PHP only (at least not the forms of URL I think of when reading clean URL). The web server needs to know how to handle requests and what requests are meant to be passed to your PHP script. Otherwise you will probably just get a 404 response.

Because the default behavior of a web server is to just take the requested URL path and try to map it to an existing file below the document root. If a corresponding file was found, either the file's content is passed back to the client or – as in case of PHP files – the file's content is passed to an appropriate interpreter and the returned data is passed back to the client. And if the file was not found, well, it responds with the status code 404. So at some point you need to configure your web server.

But after that, when the request was passed to your PHP script, you sure can use just PHP to establish clean URLs. And I would rather suggest to do that with PHP than with web server utilities. Because your PHP application should know best how to handle a requested URL.

In PHP, all required information are in the $_SERVER variable :

  • $_SERVER['REQUEST_URI'] holds the requested URL path and query (you can parse that with parse_url ), and
  • $_SERVER['PATH_INFO'] holds the PATH_INFO if you're using that (see Apache's AcceptPathInfo directive ).

Nope, no curl or framework doing this. Nor php at all.
It is web server who deal with urls.
So, if you want fake urls, you have to set up your web server to redirect certain urls to certain scripts.
The most common way is to use Apache web server with mod_rewrite module

Try to rewrite url using php and rewrite url using .HTACCESS .

For example, original url,

www.domain.com/item.php?product=Cars for sale in amazon 

with php

www.domain.com/item.php?product=Cars-for-sale-in-amazon

and with .HTACCESS file

www.domain.com/Cars-for-sale-in-amazon 

From what I have read and understand of it, there's 2 ways you can do this:

  1. The first being mod_rerite where everything seems to re fone through rewrite rules through the .htaccess file fairly simple to do but can put big load on webserver with large sites
  2. Use PHP to control the rerites this does use .htaccess but only to redirect everything back to the index.php where a dispatcher reroutes paths as necessary. There's a fantastic tutorial of this at phpvideotutorials.com the tutorial is called the tumblelog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM