Currently, file extension .php is removed correctly from my URL. But the URLs are available without trailing slash under www.example.com/thema and with trailing slash under www.example.com/thema/.
Content of my .htaccess file:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# redirect file.php to /file/
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php\s [NC]
RewriteRule ^ /%1/ [R=302,NE,L]
# added .php extension to /file by checking presence of file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
</IfModule>
Can You please give me some advice what needs to be changed that following points match:
- Domain should be available under https
- Homepage should be available only under
www.example.com(w/o trailing slash) - Subpages should be available only with trailing slash (e.g.
www.example.com/thema/
You should be redirecting to
httpsin this rule, nothttp. (The presence ofhttpin this rule will result in an additional redirect, since the first rule will then redirect you back tohttps.)You can then add another rule following this that appends the trailing slash:
The
$0backreference contains the entire match from theRewriteRulepattern (ie. the entire URL-path, less the slash prefix).There is always a slash at the start of the URL-path (ie. immediately after the hostname). You have no control over this (it is required to make a valid HTTP request). However, whether that is displayed in the browser's address bar is another matter - most browsers omit it, but that's just aesthetics (the same way browsers tend to omit the
www.subdomain and the request scheme, ie.httpandhttps, and even the query string in some cases, from the visible URL).See this question on the Webmasters SE site for more information: https://webmasters.stackexchange.com/questions/35643/is-trailing-slash-automagically-added-on-click-of-home-page-url-in-browser
Aside:
This may or may not be an issue, but this rule will fail if the request, that contains a
.phpextension, also includes a query string. To resolve that and remove the.phpextension regardless of whether a query string is present or not then you could do the following instead:Any query string on the initial request is preserved.
(NB: This should ultimately be a 301, once you have confirmed it works as intended.)
The first condition (
RewriteConddirective) is superfluous and should be removed, unless you also happen to have directories of the same name as the.phpfile basename and accessing the directory should take priority? (This seems very unlikely, since it would also make the.phpfile inaccessible from HTTP requests.)However, in order to minimise the number of redirects (not that that should necessarily be an issue here) you would need to reorder the directives and use absolute URLs in the redirects discussed above. I'm also assuming you are not implementing HSTS.
For example:
I removed the the
<IfModule>wrapper, since that is not required here (it would only serve to mask any errors if mod_rewrite is not installed).