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 would 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
Related
I've searched everywhere trying to find a solution to something I thought was a particularly common problem, but I can't seem to find anything that works.
I'm using mod_rewrite to redirect all requests via my own CMS (index.php). Here's the htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
(The above mod_rewrite is exactly the same as the code found in a standard Wordpress installation which is why I'm perplexed as to how I cannot find a solution for the following problem)
I want to add some image processing to my video thumbnails. I have written a php script (video-image.php) that works great but I cannot seem to find a means to redirect the images urls whilst keeping the index.php redirect intact. The code I'm looking for does something like this:
RewriteRule ^images/video/(.*)/(.*).jpg /images/video/video-image.php?video_image_id=$2&video_image_width=$1 [NC]
But the above code seems to clash with the mod_rewrite that sends my requests to index.php. It seems I can either redirect the images to video-image.php or redirect my pages to index.php but there must be a way to do both?
I've always found mod_rewrite confusing so apologies if I'm not explaining myself clearly enough. All php scripts function perfectly well without the mod_rewrite so I'm certain it's not a php issue.
If anyone can shed any light on this problem or point me to an answer I'd really appreciate it!
The gist of your issue was the ordering of rewrite blocks. You usually put the more specific ones above the general rules. In your case:
# specific
RewriteRule ^images/video/(.*)/(.*).jpg /images/…
# generic
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
A better approach for Apache 2.4 and later is to use FallbackResource instead of the RewriteCond/Rule blob:
RewriteRule ^images/video/(.*)/(.*).jpg /image
FallbackResource index.php
Two more things:
<IfModule mod_rewrite.c>
</IfModule>
Is something that you should not commonly use. Conditional directives make sense in Apaches core configuration. For .htaccess RewriteRule blocks it's less advisable. It's unlikely that mod_rewrite randomly disengages at runtime. And if ever, you'd rather want HTTP 500 errors in your log instead of users seeing 404 Not Found results.
Also RewriteBase can shorten some rules, or abstract their residence. But you also shouldn't use it habitually. Instead just prefix blog/ to your match rules, and assemble all rewrites in the DOCUMENT_ROOT/.htaccess (or better yet VirtualHost section for performance).
I want to show my 404 error page when ever someone gets on a non working / non created page. After a long time of searching on the web I couldnt find any solution so I post one myself. I dont have any .htaccess file either i created it filled it in but in my FTP it kept saying nothing was safed in there and it didnt show any bytes so thats something im also worried of.
So how can i get my .htaccess file to work and show my 404 Error page template? I followed all the steps to make a 404.php file but i dont mind it to be redirected to the same template I made for it but then as a page any of those solutions is fine.
You've got some rewrite rules in your .htaccess file that are preventing a 404 error from every truly happening...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
That basically states that if the requested resource is neither a file nor a directory (basically a 404 error), redirect to /index.php
Try this in your .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# delete the line below to have /index.php/anything trigger a 404
RewriteRule ^index\.php$ - [L]
</IfModule>
# END WordPress
#404 redirect
ErrorDocument 404 /httpdocs/wp-content/themes/avian/404.php
Note
This may make Wordpress quite unhappy however, I'm not sure how it uses internal routing, it may rely on breaking 404s to determine which pages to load by using fake (RESTful) URLs - reinstating 404 errors make cause Wordpress to stop working properly ... I am, however, no expert on it having barely ever touched it.
I have just had a problem with a Codeigniter site where, after the hosting company had migrated files to a new server, I could no longer navigate away from the home page. I Googled a forum with a similar issue and the answer was my htacess file. It was previously like this:
Options FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
and the given solution was to add a '?' character after 'index.php'. I did that and everything then worked OK.
I have tried hard to understand htaccess code and syntax and read many documents but they might as well be written in Chinese for all the sense I can get out of them. So can anyone explain why that additional '?' was needed and what it did? And if you can explain the rest of the code too I would be eternally grateful!
Your new host's php handler or webserver isn't setup to handle PATH INFO, or the stuff after the script (index.php) that's part of the actual path, e.g.
/index.php/this/is/part/of/the/path_info
The index.php script is executed, and everything after can be fetched via "PATH_INFO". If the server doesn't handle this, code igniter can optionally handle the path passed in as the QUERY STRING. Which are parameters that follow a ?.
None of this has anything to do with htaccess or mod_rewrite. It's just the way URLs and handlers work.
I have read lots of threads about htaccess redirect, but none really match my situation, albeit quite simple, got me pulling my hair out.
for example I have a site with this structure:
root
-gallery.php
And I have already implement htaccess rule to hide .php extension.
How do I catch the following events:
mysite.com/nonexistingfolder would go to mysite.com/404.php
mysite.com/gallery/nonexistingfolder would still load mysite.com/gallery, so that I can catch the /nonexistingfolder as a $_GET inside mysite.com/gallery.php?
Append this after RewriteRule
ErrorDocument 404 /error_docs/404
Replace /error_docs/404 with the file that shows the error or does something.
For the regular 404 page, use an error document:
ErrorDocument 404 /404.php
That covers #1, non-existing folder will go there.
For #2 try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^gallery/(\w+)$ /gallery.php?page=$1 [L]
I have a question about using multiple .htaccess files - I couldn't find the answer to this after looking elsewhere on stackoverflow, so I hope you guys can help.
I currently have one .htaccess file in the root of my site, which performs a simple url rewrite:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [L]
ErrorDocument 404 /index.php
I'm currently working on the second phase of development of this site, and I've made a replica in a subfolder (e.g. www.abcdef.com/new/). The trouble is, at the moment if I click a link on this replica site, it redirects me to the root, original page, whereas I want it to go to the equivalent page in the new/ folder. I've put another .htaccess file in this new/ folder, which however doesn't have any noticeable effect:
Options -MultiViews
# CheckSpelling off
RewriteEngine On
RewriteBase /new/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /new/index.php?url=$1 [L]
ErrorDocument 404 /index.php
So my question is: is it permissible to have another .htaccess file in a subfolder like this? And if so, why aren't the above lines working?
Thanks in advance for any ideas on this!
It's possible to have multiple .htaccess files, and the system is designed to work the way you want it to.
You're setting RewriteBase, which explicitly sets the base URL-path (not filesystem directory path!) for per-directory rewrites.
So it seems like your requests would be rewritten to /new/new/index.php, a path and directory which probably doesn't exist on your filesystem (thus not meeting your RewriteConds) and such is being redirected to your /index.php 404.
As a test, perhaps try changing the ErrorDocument to:
ErrorDocument 404 /new/index.php
If you see rewritten calls go to this then it might indeed be your RewriteBase.
You say
The trouble is, at the moment if I click a link on this replica site,
it redirects me to the root, original page, whereas I want it to go to
the equivalent page in the new/ folder.
Could it be that you are using absolute links in your pages and not relative ones? For instance if a link looks like "/sample", when in your main site it will link to http://.../sample and the same is true if the link is inside a page under "/new/". If you'd use just "sample" then that would resolve as http://..../sample or http://...../new/sample, depending on the URL of the page.
Having a second htaccess file in a subdirectory shouldn't be an issue, and as far as I can tell, your two look okay.
Are you sure the links in the site are correct? (ex, they are /new/foo, not just /foo)?