Using mod-rewrite to pass route to index.php

微笑、不失礼 提交于 2020-01-05 10:31:37

问题


I'm kind of new to mod_rewrite so I need some help from you guys.

My folder structure is:

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.

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

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.

Could you guys help me out?


回答1:


You could use an approach along these lines:

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|pages|robots\.txt)
RewriteRule ^(.*)$ /index.php?route=$1 [L]

Save this as .htaccess in your root folder.



来源:https://stackoverflow.com/questions/17021567/using-mod-rewrite-to-pass-route-to-index-php

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