Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 791
Dylan Avatar asked Feb 25 '23 15:02

Dylan


1 Answers

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
like image 196
Ben Avatar answered Mar 10 '23 12:03

Ben