Trying to do all of the following in the .htaccess
file in the most elegant way:
- redirect all non-SSL to SSL
- redirect www to non-www
- redirect all other subdomains to main domain with query string parameter as the
subdomain. Ignore any other info in the URL (302 redirect). For example:
http(s)://subdomain.example.com(/or/any/other/folder/or_file.html)
->https://example.com/results.php?q=subdomain
- redirect all main domain 404 to main domain with query string using only
the first folder or filename as the parameter. for example:
http(s)://example.com/does/not/exist/file.html
->https://example.com/results.php?q=does
Here is what I have so far and everything works except for the subdomain redirect and the SSL redirect doesn't always work with non-SSL URLs. Not sure about the order of things or if there is a way to combine some of these.
Options -Indexes
RewriteEngine On
# this rewrites all non-SSL www to main domain (SSL)
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
#redirect all 404 to query string
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/? /results.php?q=$1 [R=302,L,NC]
IMPORTANT: everything needs to be a redirect and not a rewrite.