Using .htaccess to reroute all requests through index.php EXCEPT a certain set of requests

人盡茶涼 提交于 2019-12-22 09:13:12

问题


So I just inherited a site. The first thing I want to do is build a nice little standard, easy-peezy, CMS that allows for creating a page with any URL (for example: whatever.html).

Therefore, if user hits example.com/whatever.html, it should get any db info for whatever.html and display it. This is run of the mill stuff.

My problem is that there are quite a few pages on the site (all listed in the .htaccess) that need to continue to be accessible. For instance, /Promotions is linked to promotions.php via .htaccess, and I need it to stay that way.

Anyone know how I can construct the .htaccess file to allow specific rewrites to still work but to reroute all other requests through index.php?

Currently, I just have .htaccess show a custom 404 page which in turn checks the db for the url and displays it if it exists. This is an easy solution, but I know that some people have weird browser toolbars (dumb or not, they exist :) ) that autoredirect 404s, and I'd hate to annoy my users with these toolbars by not allowing access to certain pages.

Thanks so much for your help!


回答1:


The RewriteRule for promotions should still work as it's not 404ing.

If the 404 handler is showing the page because it exists in the database then it should really be returning a 200 OK status (overriding the 404 one), so you should not get any issues with browser toolbars.

As for doing the rerouting you can do something like this:

RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*/(promotions|anotherone|somethingelse)($|/.*$) [NC]
RewriteRule ^(.*)$ /index.php?p=$1



回答2:


Here is another variant:

RewriteEngine on
RewriteRule ^/i/(.*)$ - [L] 
RewriteRule ^/css/(.*)$ - [L] 
RewriteRule ^index.php$ - [L]
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]


来源:https://stackoverflow.com/questions/4394123/using-htaccess-to-reroute-all-requests-through-index-php-except-a-certain-set-o

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