Forum Moderators: phranque
# I'm including these here just in case they're related
## Force https://www
RewriteCond %{HTTP_HOST} !^(?:www|ww2|images)\. [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/chat.cgi
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]
## Force trailing /
RewriteCond %{HTTP_HOST} !^images\. [NC]
RewriteCond %{REQUEST_URI} !^/includes
# empty implies they're on the homepage
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_URI} !(?:\..{2,4}|/)$
RewriteRule ^(.+)$ /$1/ [QSA,R=301,PT]
##### this is the main section, though
#
# only apply if the domain is example.com
RewriteCond %{HTTP_HOST} (?:www\.)?example\.(?:co|net) [NC]
# and the default parameter doesn't already exist
RewriteCond %{QUERY_STRING} !default=
# and it's not an URL that shouldn't need it
RewriteCond %{REQUEST_URI} !^/(?:ads|images|includes|cgi-bin)
# allow for the ~example in case I need it when moving to a new server
RewriteRule ^(/~example)?/(\w+)/(.*)$ $1/$3?default=$2 [QSA] ## Safety net for "default"
RewriteCond %{HTTP_HOST} (?:www\.)?example\.(?:co|net) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)default=
RewriteRule ^ - [PT] # original
RewriteCond %{QUERY_STRING} !default=
# corrected
RewriteCond %{QUERY_STRING} !(?:^|&)default=
.... pointed out that [L] isn't really applicable in the .conf files, I had to change them to [PT]. So that's where I am now.
RewriteRule ^(.+)$ /$1/ [QSA,R=301,PT]
# allow for the ~example in case I need it when moving to a new server
RewriteRule ^(/~example)?/(\w+)/(.*)$ $1/$3?default=$2 [QSA]
www.example.com/foo/bar/
Update... at the very end of the .conf file, I added this just before the ErrorDocuments:
# I'm including these here just in case they're related
## Force https://www
RewriteCond %{HTTP_HOST} !^(?:www|ww2|images)\. [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/chat.cgi
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]
# empty implies they're on the homepage
RewriteCond %{REQUEST_URI} !^$
## Force trailing /
RewriteCond %{HTTP_HOST} !^images\. [NC]
RewriteCond %{REQUEST_URI} !^/includes
# empty implies they're on the homepage
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_URI} !(?:\..{2,4}|/)$
RewriteRule ^(.+)$ /$1/ [QSA,R=301,PT]
Is the idea here that all your URLs end in / (as if they were directories, but they aren't real physical directories) rather than extensionless? This is much easier if your URLs happen not to contain literal . periods, because then you can probably reduce the whole thing to a conditionless RewriteRule ^/([^.]+[^./])$ https://example.com/$1/ [R=301,L]
replacing “example.com” with whatever you're using to support multiple hostnames on a single file. (This, incidentally, makes canonicalization a pain and may lead to chained redirects, but that's another issue.)
I think some context is missing here? As a general rule that doesn't make sense. The "L" flag IS applicable in a server context. Although "PT" (passthrough) implies "L", so [PT] is the same as [PT,L]. "PT" is required in a server context when you need the result of the rewrite to be treated as a URI and passed back through the rewrite engine - to be processed by other rewrites and Alias etc. This is the default behaviour in a directory (ie. .htaccess) context so it's not required in .htaccess. However, you shouldn't need to be using "PT" on all your rewrites (this is what causes problems for many in a directory context and you end up having to use additional directives to prevent rewrite loops etc).
# allow for the ~example in case I need it when moving to a new server
RewriteRule ^(/~example)?/(\w+)/(.*)$ $1/$3?default=$2 [QSA]
www.example.com/foo/bar/
Unless "bar/" is a valid file (seems unlikely) then this requires further rewriting I assume? (Which you imply by having to remove the PT flag.)
If you don't include the "L" or "PT" or "END" flags then processing naturally continues to the next rule... the output of the previous is passed to the next. However, when you use "PT" then (as mentioned above) the substitution string is treated as a URI and the rewriting process starts over, which can change the URL-path you are expecting to match.
## Safety net for "default"
RewriteCond %{HTTP_HOST} (?:www\.)?example\.(?:co|net) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)default=
RewriteRule ^ - [PT]
That code block doesn't really do anything except perhaps cause the rewrite engine to start over. Which perhaps suggests your directives are in the wrong order?
Is the idea here that all your URLs end in / (as if they were directories, but they aren't real physical directories) rather than extensionless? This is much easier if your URLs happen not to contain literal . periods, because then you can probably reduce the whole thing to a conditionless
RewriteRule ^/([^.]+[^./])$ https://example.com/$1/ [R=301,L]
replacing “example.com” with whatever you're using to support multiple hostnames on a single file. (This, incidentally, makes canonicalization a pain and may lead to chained redirects, but that's another issue.)
RewriteRule ^/([^.]+[^./])$ /$1/ [R=301,L]