is there an easy way to avoid creating all the folders

心不动则不痛 提交于 2019-12-12 05:28:44

问题


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


回答1:


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.




回答2:


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 ) ?>


来源:https://stackoverflow.com/questions/5678831/is-there-an-easy-way-to-avoid-creating-all-the-folders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!