disable all subdomains except pre-defined in htaccess - regex

The below code disables subdomains, but I can't figure out how to add an exception for a specific subdomain.
I searched for questions on this subject and could not find anything.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Subdomain Exceptions...
RewriteCond %{HTTP_HOST} !^widgets\.hyperbot\.tv$
# Disable Subdomains
RewriteCond %{HTTP_HOST} !^www\.hyperbot\.tv$
RewriteRule (.*) http://www.hyperbot.tv/ [R=301,L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png)$ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !(www\.)?hyperbot\.tv [NC]
RewriteCond %{HTTP_REFERER} !google\. [NC]
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteRule (.*) - [F,NC,L]
RewriteCond %{QUERY_STRING} proc/self/environ [OR]
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode.*(.*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|\%[0-9A-Z]{0,2})
</IfModule>

To disable all subdomains except one you can use:
RewriteCond %{HTTP_HOST} (^|\.)hyperbot\.tv$ [NC]
RewriteCond %{HTTP_HOST} !^(www|widgets)\.hyperbot\.tv$ [NC]
RewriteRule ^ http://www.hyperbot.tv%{REQUEST_URI} [NE,R=301,L]

try this code
RewriteCond %{HTTP_HOST} !^(www\.)?mysite\.tld$ [NC]
RewriteRule ^ - [F,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Related

Htaccess : https for all except specific domains

I have the following rules to redirect all request to https://www.mainsite.com
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Now I want to redirect http://another-domain.com to a specific folder (blog) but without https so I added this. but
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?another-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /blog/$1
RewriteCond %{HTTP_HOST} ^(www.)?another-domain$ [NC]
RewriteRule ^(/)?$ blog/index.php [L]
Even with that http://another-domain.com is still redirected to https://www.mainsite.com
Have it this way:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?!(www\.)?another-domain\.com$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?another-domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!blog/).+)$ blog/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?another-domain\.com$ [NC]
RewriteRule ^/?$ blog/index.php [L]

Redirect to www, https and add a trailing slash using .htaccess

This is my current .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?p=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=$1
RewriteRule ^([a-zA-Z0-9_-]+)/(.*)$ index.php?p=$1&a=$2
It works, but I would like to simplify it to avoid unnecessary redirects.
I need to redirect the user to the WWW subdomain, HTTPS protocol and add the trailing slash if needed, with as least as possible redirects.
Can somebody suggest a way to achieve this?
Thanks!
You can reduce it as:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^/?$ https://www.domain.com [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule ^(.+?)/?$ https://www.domain.com/$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule ^([\w-]+)/?$ index.php?p=$1 [L,QSA]
RewriteRule ^([\w-]+)/(.+)$ index.php?p=$1&a=$2 [L,QSA]

htaccess Rewrite for any domain

I have the following lines written multiple times to accommodate the multiple domains we use. Would there be a way to write this once so any domain would follow the same rule?
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+example.com/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/example.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example.com/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ example.com/index.php [L]
Try this single block of rule for all the doamins:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %1::%{THE_REQUEST} ^(.+?)::\s/+\1/(\S*)
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{HTTP_HOST}::%{REQUEST_URI} ^(?:www\.)?(.+?)::/(?!\1/) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %1/$1 [L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^/?$ %1/index.php [L]

htaccess rules for domain mapping and preserve current configuration

I have a site with dynamic subdomains based on registered usernames that are parsed using php and for that my htaccess works without a problem.
Here are some example on how the redirect works from $_SERVER
thecspace.com/blogs
["QUERY_STRING"] => string(21) "template=www&q=blogs/"
http://www.thecspace.com/blogs/test-pp-blog-2904
["QUERY_STRING"] => string(38) "template=www&q=blogs/test-pp-blog-2904"
http://danvlad.thecspace.com/blogs/test-pp-blog-2904
["QUERY_STRING"] => string(47) "template=danvlad&q=blogs/test-pp-blog-2904"
CURRENT HTACCESS
RewriteCond %{HTTP_HOST} ^thecspace\.com$ [NC]
RewriteRule ^(.*)$ http://www.thecspace.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)?\.thecspace\.com
RewriteRule ^$ index.php?template=%2 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www\.)?([^\.]+)?\.thecspace\.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?template=%2&q=$1 [L,QSA]
Now comes the hard part. I want to allow custom domains to use the same rules. Let's say devdemogroup have the domain devdemogroup.com. Once they change DNS to point to domain.com I want to be able to allow that domain to use current code from domain.com
So, in theory a request to www.danvlad.com would be a this query string
["QUERY_STRING"] => string(47) "template=danvlad"
and www.danvlad.com/blogs/
["QUERY_STRING"] => string(47) "template=danvlad&q=blogs"
I've tried this but it won't work
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.thecspace\.com$ [NC]
RewriteRule ^$ /index.php?template=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.thecspace\.com$ [NC]
RewriteRule ^$ /index.php?template=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.com$ [NC]
RewriteRule ^$ /index.php?template=%1 [L]
RewriteRule ^(.*)$ /index.php?template=%1?q=$1 [L,QSA]
Can anyone help?
Thank you
Actually your last RewriteRule is without conditions, use it like this:
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]+)\.thecspace\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?template=%1?q=$1 [L,QSA]
Final solution
Route53
thecspace.com Zone
danvlad .thecspace.com CNAME www.thecspace.com
www.danvlad .thecspace.com CNAME www.thecspace.com
danvlad Zone
www.danvlad.com CNAME www.thecspace.com
Options FollowSymLinks
AllowOverride All
Allow from all
DirectoryIndex index.php
# Redirect thecspace.com and cspace.thecspace.com to www.thecspace.com
RewriteCond %{HTTP_HOST} ^thecspace\.com$ [NC] [OR]
RewriteCond %{HTTP_POST} ^cspace.thecspace.com$ [NC]
RewriteRule ^(.*)$ http://www.thecspace.com/$1 [R,L,QSA]
# Redirect abc.thecspace.com to www.thecspace.com/index.php?template=abc&q=zzz
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.thecspace\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?template=%1&q=$1 [L]
# Redirect www.abc.thecspace.com to www.thecspace.com/index.php?template=abc&q=zzz
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.thecspace\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?template=%1&q=$1 [L]
# Redirect www.abc.com to www.thecspace.com/index.php?template=abc&q=zzz
RewriteCond %{HTTP_HOST} !^www\.thecspace\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php?template=%1&q=$1 [L]
# Redirect www.thecspace.com
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME) !^$
RewriteRule ^(.*)$ /index.php?q=$1 [L]
Maybe it helps someone.

htaccess rules: no doubles, no index, no extensions, correct redirect

I am trying to create .htaccess rules to fix all of that, here's what I have:
RewriteEngine on
Options -Multiviews
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^ru\/index\.php$ "http\:\/\/www\.example\.com\/ru\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^en\/index\.php$ "http\:\/\/www\.example\.com\/en\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^it\/index\.php$ "http\:\/\/www\.example\.com\/it\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^fr\/index\.php$ "http\:\/\/www\.example\.com\/fr\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^de\/index\.php$ "http\:\/\/www\.example\.com\/de\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^uk\/index\.php$ "http\:\/\/www\.example\.com\/uk\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^es\/index\.php$ "http\:\/\/www\.example\.com\/es\/" [R=301,L]
RewriteCond %{THE_REQUEST} \.php [NC]
RewriteRule \.php$ - [L,R=404,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
ErrorDocument 404 /en/404.php
ErrorDocument 403 /error_page.html
ErrorDocument 500 /error_page.html
Using those rules I still have problems:
When I try to access the page with .php I got an error:
http://www.example.com/en/corniglia.php
But I prefer to get a redirect to the page without extension. When I fix this - something else brakes..
Have your code this way:
ErrorDocument 404 /en/404.php
ErrorDocument 403 /error_page.html
ErrorDocument 500 /error_page.html
RewriteEngine on
Options -Multiviews
RewriteCond %{HTTP_HOST} ^(www\.)?incinqueterre\.com$ [NC]
RewriteRule ^(ru|en|it|fr|de|uk|es)/index\.php$ /$1/ [R=301,L,NC]
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?/] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]