0

I am very noob to this URL rewrite. just got a question in my head.

www.example.com/?page_name=home

The $_GET['page_name'] is actually home

after the URL rewrite the url become

www.example.com/home

can PHP still get the $_GET['page_name'] is 'home'??

Thanks

4

3 に答える 3

2

The URL rewriting is done by the web server, let's say in this case Apache. This is not the same as PHP.

Apache receives a request for the URL www.example.com/home. It now needs to figure out what to do with this request. It will check its configuration for something that matches www.example.com, which will point it to a document root, i.e. some folder on the hard disk. It checks that folder on the hard disk and encounters an .htaccess file. It evaluates the .htaccess file, which tells it to rewrite the URL from /home to ?page_name=home.

Apache now tries to figure out what to do with ?page_name=home. Since there's no filename given, it defaults to index.php (which hopefully exists). It now runs that index.php file in the document root, passing it ?page_name=home as the URL it has received. PHP takes it from there, oblivious of the rewriting that happened. To PHP, it appears that you have received the parameter page_name as query parameter and puts it in $_GET.

于 2012-05-22T06:36:08.137 に答える
1

It depends on the rewrite rule, but yes, you can get it to work as intended.

The following rewrite rule:

RewriteRule /home /index.php?page_name=home

Will simply cause requests to /home to execute index.php with $_GET['page_name'] equal to "home".

Depending on the complexity of your site, however, it may be preferable to use a more generic rewrite rule, such as:

RewriteRule ^(.+)$ index.php/$1

Then you would query $_SERVER['PATH_INFO'] to see if it contained "home". This will play nicely with other $_GET parameters that may be passed in.

于 2012-05-22T06:38:52.877 に答える
1

Try using rewriting like this (change it as per your need):

RewriteRule ^([A-Za-Z0-9-_]+)/?$ index.php?page_name=$1 [L]

Above rule redirects

http://www.domain.com/string_LiKe-this53/

on a real existing page

http://www.domain.com/index.php?page=string_LiKe-this53

on which you can use your $_GET['page'], which will have value string_LiKe-this53.

于 2012-05-22T06:42:48.113 に答える