I am using .htaccess to enforce all www and http requests to non-www https, and adding the trailing / at the end.
Also, I am redirecting all the requests to index.php from where I am handling the routes.
What I had been doing was, checking for routes url and serving the files in index.php. And, also handling the 404 page from there.
However, I came to know it was not good a way to do it.
I tried doing it with ErrorDocument, but I am facing problem, as I am handling all URL to file thing from index.php and, I am having difficulty in combining RewriteRule and ErrorDocument.
Also, am I doing this correctly, or if there is any good approach to do it.
Here is my .htaccess:
<IfModule mod_rewrite.c>
# Remove www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# # Force HTTPS
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
# Turn on rewrite engine
RewriteEngine on
# Adds Trailing slash
#RewriteCond %{REQUEST_URI} !(/$|\.|^$)
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
ErrorDocument 404 thisshouldleadtoerrorpage
</IfModule>
And this is in the index.php
$url_requested = $_SERVER['REQUEST_URI'];
$url_len = strlen($url_requested);
$actual_path = substr($url_requested, strpos($url_requested,'/'), $url_len);
// HOME PAGE
if($actual_path == "/"){
include_once( 'pages/home.php');
}
Could anyone help me with this.
All I want is a custom 404 page satisfying above conditions. Thanks.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.