0
\$\begingroup\$

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.

asked Jul 5, 2024 at 9:05
\$\endgroup\$
3
  • \$\begingroup\$ Is this your complete .htaccess file? Presumably you also have a RewriteEngine directive? \$\endgroup\$ Commented Jul 5, 2024 at 9:58
  • \$\begingroup\$ This is only a segment of my .htaccess file. \$\endgroup\$ Commented Jul 5, 2024 at 10:04
  • 1
    \$\begingroup\$ Then we would need to see your complete .htaccess file. You are missing the L 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\$ Commented Jul 5, 2024 at 14:52

1 Answer 1

3
\$\begingroup\$

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.

answered Jul 5, 2024 at 10:19
\$\endgroup\$
1
  • 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\$ Commented Jul 5, 2024 at 10:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.