LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   hiding file extension with htacces gives Internal Server Error? (https://www.linuxquestions.org/questions/linux-server-73/hiding-file-extension-with-htacces-gives-internal-server-error-4175672853/)

jag1 04-09-2020 07:43 AM

hiding file extension with htacces gives Internal Server Error?
 
Hello,

I get
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
on every single image, js file and css file. How can i fix this issue?


Options -MultiViews
RewriteEngine On

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

RewriteRule ^knowledge-center/(\d+)(?:/.*)?$ knowledge-center-desc.php?id=$1 [QSA,L,NC]

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

ServerSignature Off


DirectoryIndex schedule.php

DocRoot 08-03-2020 10:28 AM

Quote:

Originally Posted by jag1 (Post 6109516)
RewriteRule ^knowledge-center/(\d+)(?:/.*)?$ knowledge-center-desc.php?id=$1 [QSA,L,NC]

You don't state what the URLs are that are being requested (for the images, CSS and JS files). However, the above rewrite would also catch images, CSS and JS files if they are of the form "/knowledge-center/123/image.jpg" etc. And these would all be rewritten to "knowledge-center-desc.php" - although this would probably just result in a bunch of 404s, not a 500.

Quote:

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

However, this could result in a rewrite-loop (500 error) depending on the URL being requested, since the file check (2nd condition) is not necessarily the same as the URL being rewritten to. The first condition (that checks whether the request maps to a directory) is most probably superfluous. You should change this to:

Code:

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

You could also exclude (at the top of the file) all requests that look like images, CSS or JS files from being rewritten - this would be a good optimisation anyway. For example, add the following at the top of the .htaccess file, immediately after the RewriteEngine directive:

Code:

# Ignore any requests that contain an image, css or js file extension
RewriteRule \.(jpg|png|gif|css|js)$ - [L]

This then prevents images, css and js files from being processed any further - which should also prevent these resources from triggering a rewrite loop (most probable cause of the 500 error).


All times are GMT -5. The time now is 07:15 AM.