Importing the right file when I'm rewriting ur

2019-08-20 08:30发布

问题:

I made all request to my site pass trough one index.php file, if a user enters www.example.com/products/20 I want my index.php file to import and render the file /products/index.php and then still be able to use product_id=20 parameter. Each time I try to access www.example.com/products/20 my index.php file seems to try to import /products/31 which does not exist instead of /products/index.php.Am rewriting my url with .htaccess is there any way I can import the right page from this url (i.e example.com/products/20)?

回答1:

Identifying your requirements

You have product records. To view the 20th record, you need to pass product_id=20 to your script. To do that, you can either pass it in a query string ?product_id=20 or you can craft a search engine friendly url /product/20. The latter requires url rewriting, usually in .htaccess.

Don't get confused-- .htaccess does not create the search engine friendly url (SEF); it interprets it and passes control to some page, ie, /index.php.

There are a multitude of tutorials on how to craft .htaccess to do this; generally it will look like this:

Options +FollowSymLinks

RewriteEngine On
RewriteRule ^topic/([a-zA-Z0-9]+)/$ index.php?topic=$1

In your case, you would simply substitute ^product for ^topic and ?product_id for ?topic. Oh, and change the regex to [0-9]

What will this accomplish?

Now, when a request that comes in as /product/{someNumber}, mod_rewrite will translate that to /index.php?product_id={someNumber}. So if you type www.example.com/product/20 or www.example.com/index.php?product_id=20, they will both go to the same script.

Note: only URIs matching product at the beginning will be affected. If you typed in www.example.com/users/20 then the web server would look for an index file in {documentRoot}/users/20

Using index.php

For all URIs starting with /product, you can now use index.php as a controller. In other words, you can use the parameters passed in to determine what the script does from there-- what logic to load, what views to use, etc.

In this example, it's pretty simple; everything coming to index.php will be a product view.

<?php
require("../views/header.php");
$query = "select * from products where product_id = ?";
// do your database call and print out results
require("../views/footer.php");

note, I always have component parts in directories NOT in the documentRoot path (../views/). This way, they are inaccessible via URL.

But what about www.example.com/

Yep, it will try to print out a product page with no product id. So if you want to have something other than a product page on your index page, just change your .htaccess file to point to some other filename.

RewriteRule ^product/([0-9]+)/$ my_product_display_page.php?product_id=$1

More dynamic pages

You aren't limited to just products. Say you want users, too. Just add more parameters.

RewriteRule ^page/([a-zA-Z]+)/([0-9])$ my_simple_controller.php?action=$1&value=$2

and in my_controller.php,

require("../views/header.php");
switch($action) {
  case 'product':
    // display product
    break;
  case 'user':
    // display user
    break;
}
require("../views/footer.php");

Now, everything that starts with /page/product will display a product, and /page/user will display a user.

Hope that gets you pointed in the right direction.