开发者

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

开发者 https://www.devze.com 2023-01-29 08:30 出处:网络
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).

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 allowi开发者_开发技巧ng access to certain pages.

Thanks so much for your help!


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


Here is another variant:

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

精彩评论

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