is there an easy way to avoid creating all the fol

2019-09-14 02:55发布

Ok so I have this site.... with the url

http://posnation.com/

and there are alot of pages that i need to save the url structure for....like this

http://posnation.com/restaurant_pos
http://posnation.com/quickservice_pos
http://dev.posnation.com/retail_pos

ext....

The problem that i have now is that i want to save the same url for all these pages and I am looking for the best approach. The way its working now is its done with a code in miva and we are getting off miva.... I know I can create a folder named restaurant_pos or whatever the url is and create an index.php in there.This approach will work but the problem is I need to do this for 600 different pages and I dont feel like creating 600 folders in the best approach.

any ideas

2条回答
【Aperson】
2楼-- · 2019-09-14 03:20

You should use .htaccess to route all the requests to a single file, say index.php and do the serving from there based on the requested URL.

The following .htaccess file on the server root will route all the requests to your index.php:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L]
</IfModule>

Now you should parse the $_REQUEST['route'] to identify which file you should serve. Here is an example that will serve a page based on the last element of the URL (ex: pos):

<?php

$parts = explode($_REQUEST['route']);

if ($parts[count($parts) - 1] == 'pos') {
    include "pages/pos.php";
}

Definitely you'll need to write your own logic, the above is just an example.

Hope this helps.

查看更多
Viruses.
3楼-- · 2019-09-14 03:32

Usually the easiest way to do this is to create an .htaccess file that redirects all requests to /index.php. In /.index.php you analyze the URL using probably $_SERVER['REQUEST_URI'] and include the appropriate content.

Heres a sample htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

In your /.index.php do something like ... (this is just a VERY simple example)

require 'pages/' . $_SERVER['REQUEST_URI'] . '.php';

Now if someone goes to http://posnation.com/restaurant_pos pages/restaurant_pos.php will be included.

pages/restaurant_pos.php could include the header and footer too.

<?php require( HEADER_FILE ) ?>
restaurant_pos content
<?php require( FOOTER_FILE ) ?>
查看更多
登录 后发表回答