0

so I have an issue with htaccess file. So if I try to open an image that doesn't exists, it redirects me to 404 page which is fine. but if I try to open a php page that doesn't exists, it says "file not found" instead of redirecting to 404 page. Any idea why is that? Below is the code I used in htaccess file

ErrorDocument 404 https://www.mywebsite.co.uk/404

Below is the full htaccess file. This was created by previous developer. I am not familiar with htaccess coding so I decided to post the issue here.

order allow,deny
allow from all
RewriteEngine on

RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://www.mywebsite.co.uk/$1 [R,L]

RewriteCond %{HTTP_HOST} ^mywebsite.co.uk [NC]
RewriteRule ^(.*)$ https://www.mywebsite.co.uk/$1 [L,R=301]

RewriteRule ^index\.php$ / [R=301,L]
RewriteRule ^(.*)/index\.php$ /$1/ [R=301,L]

ErrorDocument 404 https://www.mywebsite.co.uk/404
RewriteCond %{HTTP_REFERER} !^https://www.mywebsite.co.uk/.*$      [NC]
RewriteCond %{HTTP_REFERER} !^https://www.mywebsite.co.uk$      [NC]
# RewriteRule .*\.(jpg|jpeg|gif|png|bmp)$ - [F,NC]


RewriteRule ^job/([^/\.]+)/?$ ./job-display.php?job_id=$1 [L]
RewriteRule ^job/([^/\.]+)/([^/\.]+)/?$ ./job-display.php?job_id=$1 [L]
RewriteRule ^jobs/([^/\.]+)/?$ ./job-display.php?jobg8_id=$1 [L]
RewriteRule ^jobs/([^/\.]+)/([^/\.]+)/?$ ./job-display.php?jobg8_id=$1 [L]

RewriteRule ^location/([^/\.]+)/?(.*)$ /search_result.php?jobtitle=&joblocation=$1 [L]

RewriteRule ^company/([^/\.]+)/?$ ./search-result.php?adv_name=$1&jobtitle=$2&joblocation=$3 [L]
RewriteRule ^company/([^/\.]+)/([^/\.]+)/?$ ./search-result.php?adv_name=$1&jobtitle=$2&joblocation=$3 [L]

RewriteRule ^organisation/([^/\.]+)/?(.*)$ ./search-result.php?organisation=$1 [L]

RewriteRule ^featured/([^/\.]+)/?$ ./search-result.php?user_id=$1 [L]
RewriteRule ^featured/([^/\.]+)/([^/\.]+)/?$ ./search-result.php?user_id=$1 [L]

RewriteRule ^jobs/apply/([^/\.]+)/?$ ./apply-job.php?job_id=$1 [L]
RewriteRule ^jobs/apply/([^/\.]+)/([^/\.]+)/?$ ./apply-job.php?job_id=$1 [L]

AddType x-httpd-php54 .php
# Added automatically by Gridhost panel Tuesday 23rd of May 2017 08:54:14 PM
Options -Indexes


RewriteRule ^(employer-admin|admin)($|/) - [L]

# Hide php extension of remaining files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]




# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php5_module>
   php_flag asp_tags Off
   php_flag display_errors Off
   php_value max_execution_time 900
   php_value max_input_time 6000
   php_value max_input_vars 1000
   php_value memory_limit 1024M
   php_value post_max_size 8M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php54"
   php_value upload_max_filesize 200M
   php_flag zlib.output_compression On
</IfModule>
<IfModule lsapi_module>
   php_flag asp_tags Off
   php_flag display_errors Off
   php_value max_execution_time 900
   php_value max_input_time 6000
   php_value max_input_vars 1000
   php_value memory_limit 1024M
   php_value post_max_size 8M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php54"
   php_value upload_max_filesize 200M
   php_flag zlib.output_compression On
</IfModule>
# END cPanel-generated php ini directives, do not edit

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php73” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
aslamdoctor
  • 3,753
  • 11
  • 53
  • 95
  • You have enabled mod_rewrite module of apache right? – Vishal Mar 31 '20 at 04:54
  • ErrorDocument 404 https://www.mywebsite.co.uk/404 at the top should work. Please see https://stackoverflow.com/questions/19962787/rewrite-url-after-redirecting-404-error-htaccess – M.D Mar 31 '20 at 05:23
  • _Redirecting_ to a 404 document is **fundamentally wrong** to begin with. Search engine bots will follow that redirect, get a 200 OK response code from the new URL then - and won’t be able to properly recognize that the _originally_ requested URL does not exist. – CBroe Mar 31 '20 at 08:39
  • I guess it might have to do with the `# Hide php extension of remaining files` section. This rewrites `foo` to `foo.php`, if `foo` itself does not exist - but what if `foo.php` does not actually exist either? I’d try and add a second condition, that checks if `foo.php` exists, and only then does the rewrite - `RewriteCond %{REQUEST_FILENAME}.php -f` – CBroe Mar 31 '20 at 08:43

1 Answers1

0

I fixed the issue. The rule to hide .php extension had a big. It says !-f but it is supposed to be -f and then the ErrorDocument statement needs to be added in the next line. So the code is like this.

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

ErrorDocument 404 https://www.mywebsite.co.uk/404

I have no idea how it works in theory. This is something I found from another website and tried to use this solution. Which worked :)

Thank you all for the feedback.

aslamdoctor
  • 3,753
  • 11
  • 53
  • 95