John Davidson

.htaccess - Pages are corrupted once using include(404.php); after sending http_response_code(404)

0 comments
Message:


I have set up a mod_rewrite in a sub-directory (NOT root directory) using .htaccess file, code as follows:


#Enable the mod_rewrite
RewriteEngine On

#Rewrite condition as follows, if the URL matches a read directory, file or link then don't do the rewrite.
ReweiteCond %{REQUEST_FILENAME} !-d
ReweiteCond %{REQUEST_FILENAME} !-f
ReweiteCond %{REQUEST_FILENAME} !-l

#The Rewrite rule is as follows:
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]

The plan is if the user enters a wrong URL a 404 error to be returned. I have used the following PHP code that worked with me perfectly in a PHP file located in the root directory.


http_response_code(404);
include("404.php");
die();

However, the result is not expected, the page appears as follows:



L���O�����;�γ}��Li[zf˫9]�!i$a����_��ZBU)��,[@y�O$�������'��3�I�m���K���J,b����u�������&�����Im���:�=�A���A���$�d&:����4�`L8�?��ѾQ|��~��"֡2\�Uq�^��~ӫ�=��hO�\uJ��ӳ��0.�L�e��HgtX7��3:l����x�~�@�!Jgt�z3
����ϼ�T�$O�'�6�~�������.��s~A�e
�� e�=ذ_2����wfԢ��,��,-�
)�3�3S��Ll���u��\��%[�5['�U
Ϸ�Ƅ3g�5�S��q�|�&-G����-6��O<��uƛ.��y���w�v(W*���Yʁ��7��r�j3q������c�~�g��Yฺ"$߼ˊ�1cv����;�C��Ҹ3�
h��<�>:��5xޡsP/u㾾



I'm not sure if this has anything to do with the .htaccess, but I have put everything that I thought is related.


Please note that both pages either the one that calls 404.php and the latter has the charset=UTF-8



My Answer:
RewriteEngine On
RewriteBase /subdirectory/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subdirectory/index.php [L]

Rate this post

3 of 5 based on 4802 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us