开发者

how to write this 404 rule in .htaccess

开发者 https://www.devze.com 2023-04-10 10:45 出处:网络
Ihave a custom CMS. It works very well except for one thing. I am wondering how best to implement 404 functionality. The site should only have the following url types

I have a custom CMS. It works very well except for one thing. I am wondering how best to implement 404 functionality. The site should only have the following url types

index.php?upn=something

or

gallery.php?groupId=2

Any directories that are appended to the url after index.php or gallery.php should throw 404's. Also I w开发者_如何转开发ould like to throw a 404 if the content is not found in the database when a upn is specified. I realise that I can throw the header in php. But I said I would try to get a little more insight into this problem overall. So if ye have any info or pointers I'd appreciate them.


I like to do this programatically from my PHP code. You could write some simple Route Component, that try to find what you want. Today you're routing just to actions (index.php?upn= and gallery.php?groupId=) but someday in the future you may add new one, and you should change your .htaccess, which is more difficult. If you do it all in your app tier you can change it simpler.

Paste this code in a .htaccess file at the root of your project, and will route every request to router.php. Once you have the request you can decide what action to do.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ router.php?url=$1 [QSA,L]
</IfModule>

Hope it helps.


You'd have to return the 404 in the php code, but you can set the 404 handler like so in htaccess:

ErrorDocument 404 /errors/notfound.html
0

精彩评论

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

关注公众号