开发者

How to make URLs in a website appear solely as folders

I am trying to design a folder structure for a website project I am working on. A lot of sites thes开发者_运维问答e days seem to have the following link structure:

www.example.com/news/news-item-one/ www.example.com/about-us/

Can I make my site work like this without making a new folder for each page I have and putting an index.php file in it?

i.e www.example.com/news/new-item-one.php reads www.example.com/news/news-item-one/


You can use a web application framework like CodeIgniter or CakePHP to do URI routing for you:

  • http://codeigniter.com/

  • http://cakephp.org/

This is done with an .htaccess file which either of those frameworks can provide in example documents and they have extensive documentation about URI routing. For example on CI:

http://codeigniter.com/user_guide/general/urls.html


Yes, but it depends what server you are running.

If Apache, one of the most common ways is to create an .htaccess file and use rewrite rules to declare the different routes your website uses.

Below is a very simple example, although not necessarily the best way. There are things you can do to make it more flexible, but I believe it's out of scope of this question. For what it's worth, I prefer a catch-all route that passes route handling to my framework.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^news/(.*)$ news.php?item=$1 [NC,L,QSA]
    RewriteRule ^about-us/$ about-us.php [NC,L,QSA]
</IfModule>


You want to use htaccess.

Create a file in your root directory called .htaccess with the following

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ $1.php [NC]


You don't need to use a framework (although it may be a good idea). You can simply setup your .htacess correctly:

http://www.evolt.org/Making_clean_URLs_with_Apache_and_PHP


The following mod_rewrite code (to put in your Apache configuration) will allow you to hide the .php extension of any page you have on your site.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php [L]

It checks to make sure something isn't a file or directory itself, then that adding .php after it's name actually is a file, and it serves that instead.

So if you have a /page.php on your site, going to /page will be the same as going to /page.php.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜