使用mod-rewrite将路由传递给index.php [英] Using mod-rewrite to pass route to index.php
问题描述
我是mod_rewrite的新手,所以我需要你们的一些帮助.
I'm kind of new to mod_rewrite so I need some help from you guys.
我的文件夹结构是:
assets/fonts/ Contains all fonts
assets/images/ Contains all images
assets/scripts/ Contains all JavaScript files
assets/styles/ Contains all CSS files
pages/ Contains parts of the page as PHP file or directory containing PHP files
header.php Header of site
footer.php Footer of site
index.php index of site, now always loads pages/home.php.
pages目录包含页面的所有正文部分,例如home.php,about.php等.它还包含目录(例如"portfolio"),而目录又包含更多的.php文件.
The pages directory contains all the body parts of the pages like home.php, about.php etc. It also contains directories like 'portfolio' which in turn contains more .php files.
这就是我希望重新编写网址的方式.
This is how I'd like my urls to be re-written.
http://domain.com/about => http://domain.com/index.php?route=about
http://domain.com/about/kittens => http://domain.com/index.php?route=about/kittens
但是,这应该排除直接发送到assests
或pages
文件夹的请求.
在诸如http://domain.com/pages/about
之类的直接请求中应排除pages文件夹的原因是因为网站的某些部分已加载AJAX.
最好将其排除在根目录之外.
This should however exclude requests directly to the assests
or pages
folder.
The reason why the pages folder should be excludes on direct requests like http://domain.com/pages/about
is because some parts of the site are loaded with AJAX.
Preferable it would exclude any directory in the root.
你们能帮我吗?
推荐答案
您可以按照以下方式使用方法:
You could use an approach along these lines:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|pages|robots\.txt)
RewriteRule ^(.*)$ /index.php?route=$1 [L]
将其另存为.htaccess
在您的根文件夹中.
Save this as .htaccess
in your root folder.
这篇关于使用mod-rewrite将路由传递给index.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!