0

I'm having a little issue with my php htaccess 404 redirect - it's not redirecting.

I have my htaccess setup to strip the extension off the filenames/pages, and I'm wondering if that's the cause to my issue. However, I'm stuck and can't seem to figure this out.

Right not it just keeps redirecting all errors to the index page.

I've tried this suggestion with no luck -Rewrite URL after redirecting 404 error htaccess

Any suggestions?

Here's my htaccess:

Options -Indexes +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www.sitename.com$ [NC]
RewriteRule ^(.*)$ http://sitename.com/$1 [R=301,L]

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}.php -f
#RewriteRule ^(.*?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index(\?|\ )
RewriteRule ^ /%1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule . index [L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]*/)*index\.(html?|php[45]?)(\?[^\ ]*)?\ HTTP/
RewriteRule ^(([^/]*/)*)index\.(html?|php[45]?)$ http://sitename.com/$1 [R=301,L]


#if query string is empty
RewriteCond %{QUERY_STRING} ^$
#match on / for root, or .index.php in request and send to query string version 
RewriteRule ^invoices.php?$ /invoices?status=open [R=301,L] 
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^projects.php?$ /projects?status=open [R=301,L] 

#RewriteRule ^(/|invoices.php)?$ /invoices?status=open [R=301,L]
#RewriteRule ^(/|projects.php)?$ /projects?status=open [R=301,L]


ErrorDocument 400 /errors.php
ErrorDocument 403 /errors.php
ErrorDocument 404 /errors.php
ErrorDocument 405 /errors.php
ErrorDocument 408 /errors.php
ErrorDocument 500 /errors.php
ErrorDocument 502 /errors.php
ErrorDocument 504 /errors.php
JonnyPlow
  • 187
  • 1
  • 8
  • You're rewriting every non-file, non-directory request to `index.php` so obviously there is no `404` happening – anubhava Sep 19 '18 at 14:24
  • @anubhava I have to admit, I really don't understand htaccess stuff all that much (at all). I'm guessing then you're referring to the 2 blocks that have "index" in them? Could you point me in the right direction for how to modify correctly? – JonnyPlow Sep 21 '18 at 02:41
  • Okay, I just tried it by replacing "index" with "errors" (errors.php), and it seems to be working, but is that actually correct? I ask because it's not producing a 404 server status `$_SERVER['REDIRECT_STATUS'];`, but instead a 200 – JonnyPlow Sep 21 '18 at 02:55
  • That's right. If you want `404` status then remove `RewriteRule . index [L]` rule (with 2 `RewriteCond` lines above) and just keep `ErrorDocument 400 /errors.php` – anubhava Sep 21 '18 at 09:54
  • @anubhava Great, seems to be working now. Thanks for pointing me in the right direction! – JonnyPlow Sep 22 '18 at 18:35

0 Answers0