Here are two examples of what I would like to do:
Example 1: I have a services.php file. I would like it to be accessible on example.com/services. Furthermore, I would like example.com/services.php to redirect to example.com/services.
Example 2: I have index.php file. I would like it to be accessible on example.com. Furthermore, I would like example.com/index.php and example.com/home to redirect to www.example.com .
I have been looking online for a solution but could not find anything. I came up with the following Apache code for my .htaccess file:
RewriteRule ^services$ services.php [END]
RewriteRule ^services.php$ /services [R=301]
RewriteRule ^$ index.php [END]
RewriteRule ^home$ / [R=301]
RewriteRule ^index.php$ / [R=301]
The code above does the job, but I am unsure if that is the optimal solution. In the best-case scenario, I believe that there should be no redirects when accessing example.com/services and example.com, and only a single redirect when accessing example.com/services.php, example.com/index.php and example.com/home. Provided that I am using the network panel correctly, my solution seems to be behaving (what I believe is) optimally. Still, I might be overlooking something. If someone spots a problem or if there is a more template way of doing this, please let me know.
1 Answer 1
As I mentioned in a comment on your StackOverflow question: Your solution is very specific, it only works for those two cases. You could create a more general rule.
Something like: "Every url without an extension, that is not a existing file or directory on the server, should get a .php extension.".
This could look something like this in your .htaccess:
# enable runtime rewriting engine
RewriteEngine On
# if no extension, and is not a directory or file, rewrite as .php and stop
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ 1ドル.php [QSA,L]
Note that this doesn't involve redirecting, it does a rewrite of the requested URLs on the fly. That's why we need RewriteEngine On
.
The first two RewriteCond
lines check if the requested directory and file do not exist, and if that's the case the RewriteRule
line is executed. That line checks for URL's without a dot .
in it. If it finds such an URL it adds .php
to it.
Now the /home
URL won't refer to the index.php
script, because it would rewrite that URL to /home.php
. I suggest that for this exception you create a home.php
script that simply includes index.php
.
Asking for extra features isn't what Code Review is for, but I think you already know how to add case insensitivity.
-
1\$\begingroup\$ For an even more general solution most PHP programmers use a php routing library. Examples are: phprouter.com and php.libhunt.com/libs/router I've never used any of these, so I cannot vow for any quality. Often the routing library is part of a more complete framework. \$\endgroup\$KIKO Software– KIKO Software2024年07月05日 10:29:17 +00:00Commented Jul 5, 2024 at 10:29
RewriteEngine
directive? \$\endgroup\$.htaccess
file. You are missing theL
flag on your existing redirect directives - this "works" with what you have posted, but could be an issue (and certainly not "optimal") when combined with other directives. \$\endgroup\$