: How to remove trailing slashes from URL with .htaccess? The situation Across the entire domain, we'd like the URLs to hide file extensions and remove trailing slashes, independent of the domain
The situation
Across the entire domain, we'd like the URLs to hide file extensions and remove trailing slashes, independent of the domain name itself (as in, works on any domain).
Sample of our directory structure
We're not using index.* files except for the homepage.
/
/index.php
/account.php
/account
/subscriptions.php
/login.php
/login
/reset-password.php
The goal
Some examples of how these files might be requested, and how they should look in the browser:
/ and index.php --> mydomain.com (literally just the bare domain name).
/account.php or /account/ or /account --> mydomain.com/account
/account/subscriptions.php or /account/subscriptions/ or /account/subscriptions --> mydomain.com/account/subscriptions
As you can see, there are several ways to access each webpage, but no matter which of the 2 or 3 ways you use to get there, it only shows the one preferred URL in the browser.
The question
How is this done with .htaccess using mod_rewrite?
I've banged my head against the wall trying to figure this out, but in general, the rewrite flow would seem to be something like this:
External 301 redirect ( mydomain.com/account/ --> mydomain.com/account )
Internally append .php ( mydomain.com/account --> mydomain.com/account.php )
I've been Googling this all day, read thousands of lines of documentation and config texts, and have tried several dozen times... I think more brains on this would help a lot.
UPDATE
We found an answer our question (see below).
More posts by @Dunderdale272
3 Comments
Sorted by latest first Latest Oldest Best
Thanks for your time to look at the question, but we appeared to have figured it out:
Options -Multiviews -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
DirectorySlash Off
# remove trailing slash
RewriteRule ^(.*)/(?.*)?$ [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([w/-]+)(?.*)?$ .php [L,T=application/x-httpd-php]
We have to turn off Multiviews and Indexes so the engine doesn't get confused and instead try to reference any index.* file or show a directory listing (also confusingly called an "index" with Apache...) when a directory appears to be requested.
The first redirect visibly (R=301) removes the trailing slash, and the second one internally rewrites it to the PHP (or HTML, etc.) file at hand.
This .htaccess file also supports query strings.
Update As noted in the comments below much earlier, we've switched to nginx, and this is all our conf file contains related to URL-rewriting (from my dev box):
location = / {
index index.html;
}
try_files $uri $uri.html =404;
We've also switched from PHP to just plain HTML, but changing the extensions above should hardly make a difference, if at all.
If you would like a flexible and simple way to route URL requests for your website or app, I would recommend a PHP micro-framework. Not only will you have complete control over your URL routing, but it will provide other benefits as well.
I've used the Slim Framework before, combined with the Symfony Templating Component with great results. If I were to do it again, I would probably just use the Silex Micro-framework, since it's also a part of Symfony Components.
Why do you want to redirect /account to /account.php? In fact, the page /account only exists with Content negotiation. If you don't want it, just disable the directive.
About your two rules, I think it is straightforward:
RewriteRule ^/account/$ /account
RewriteRule ^/account$ /account.php [R,L]
It is untested, however. You can also add [R,L] to the first line, in this case, the browser will make one more redirect.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.