.htaccess force 404 - .htaccess

Attempting to restrict access to Addon Domains through the Main Domain by forcing 404 but doesn't seem to be working, here's what I have:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/domain2.com/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/domain3.co.uk/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/test/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
The directory test is 404'd correctly, so I'm thinking it's something to do with the .com and .co.uk characters?

Related

htaccess dynamic subdomains but redirect main domain to https-www

I am using opencart e-commerce package and have my main domain redirect to use https & www but i require some sub domains, so far in my .htaccess i have
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.co.uk
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.co.uk$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomains/(.*)
RewriteRule ^(.*)$ /subdomains/%1/$1 [L]
This works fine and i can create new sub domains in the folder called subdomains - but if a folder is not found it shows a 500 error i have tried adding in checks for folders and files but this still returning 500 errors with the error -
Request has exceeded the limit of 10 internal redirects
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.co.uk$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomains/(.*)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /subdomains/%1/$1 [L]
What can i use that will only show the subdomain if the folder and file is there otherwise redirect the request back to the main domain?
So I have managed to stop the 500 errors and show a 301 moved permanently page rather then a redirect - for some reason I cannot figure out how to redirect back to the main domain if the directory doesn't exist
## redirect the main domain to www-https
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.co.uk
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
## check the subdomain folder
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.co.uk$
RewriteCond %1 !^(www|ftp|mail)$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomains/(.*)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ /subdomains/%1/$1 [L]
## show the index.php file in the subdomain directory
## doesn't work but shows a 301 page ?
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.co.uk$
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*) /subdomains/index.php [R=301]

How do I redirect specific URL's in the .htaccess file

I want to redirect specific pages only to the mobile version of my website. I can redirect the homepage with the code below but I can't find a way to redirect specific pages. I want to redirect https://www.richmond-medical.com/sm3 to http://m.richmond-medical.com/sm3 .
My problem is when I type in Richmond-medical.com/sm3 on a mobile it goes to http://richmond-medical.com only.
Here is my code:
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} ^www.Richmond-Medical.com$ [NC]
RewriteCond %{REQUEST_URI} ^/sm3$
RewriteRule ^/?a/(.*)$ http://m.richmond-medical.com/$ [R=301,L]
RewriteCond %{HTTP_HOST} richmond-medical.com [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.*)$ http://m.richmond-medical.com [L,R=301]
</IfModule>
The working code for the above requirement is below
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(R|r)ichmond-medical.com$ [NC]
RewriteCond %{REQUEST_URI} ^/sm3$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://m.richmond-medical.com/$1 [R=301,L]
I have clubbed both the rule in an [OR] clause so that root domain redirects to mobile one and sm3 redirects to mobile one.
The .htaccess rule can be tested at .htaccess tester
In case you have some new pages that needs to be redirected to mobile one you can add it to the [OR] clause like if we need sm5 to be redirected we just need to add sm5 in [OR] clause. We can also club it in the same rewrite condition using | like below.
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(R|r)ichmond-medical.com$ [NC]
RewriteCond %{REQUEST_URI} ^(/sm3|/|/sm5)$
RewriteRule ^(.*)$ https://m.richmond-medical.com/$1 [R=301,L]

How to redirect one visitor IP address to a specified page on my website

I'm trying to use .htaccess to redirect a visitor with a specified IP address to always be redirected to a specified page when visiting any page on my website. I've tried the code below but it is causing an error. I'm using a wordpress site.
RewriteCond %{REQUEST_URI} !/specified-page/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteCond %{REMOTE_HOST} !^1\.2\.3\.4
RewriteRule $ /specified-page/ [R=302,L]
It is causing "ERR_TOO_MANY_REDIRECTS" message.
As a side-note, my domain is pointing to a sub-folder in my webspace. As are some other websites. This is all directed from another .htaccess file in the root of my webspace with code like this.
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder1/
RewriteRule (.*) /mywebsitefolder1/$1 [L]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder2/
RewriteRule (.*) /mywebsitefolder2/$1 [L]
and so on...
(The .htaccess I'm trying to edit to create the IP redirect is in 'mywebsitefolder1' folder.)
Try it like that:
RewriteCond %{REQUEST_URI} !/specified-page/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteCond %{REMOTE_HOST} !^1\.2\.3\.4
RewriteRule ^ /specified-page/? [R=302,L]
This will also remove an optional query string that could cause the redirect loop.
You have indeed a looping condition. Let's try it like this:
RewriteCond %{REMOTE_HOST} ^1\.2\.3\.4
RewriteCond %{REQUEST_URI} !/specified-page/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif|css|js) [NC]
RewriteRule ^ /mywebsitefolder1/specified-page/ [R=302,L]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain1\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder1/
RewriteRule (.*) /mywebsitefolder1/$1 [L]
RewriteCond %{HTTP_HOST} ^([^.]+\.)?mydomain2\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mywebsitefolder2/
RewriteRule (.*) /mywebsitefolder2/$1 [L]

How do I redirect subdomain to subdirectory (with default folder) on htaccess without change address

I'm trying to do a Rewrite subdomains to subdirectories on htaccess.
Searching here I did the rule below:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com\.br
RewriteCond %{REQUEST_URI} !^/sd_
RewriteRule ^(.*)$ http://domain.com.br/sd_%1/$1 [P,L,NC,QSA]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.(.*)\.domain\.com\.br
RewriteCond %{REQUEST_URI} !^/sd_
RewriteRule ^(.*)$ http://domain.com.br/sd_%1/$1 [P,L,NC,QSA]
#Rewrite for my Framework
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
When I access the address: http://user.domain.com.br or the address: http://www.user.domain.com.br they're redirected without change the URL address to: http://www.domain.com.br/sd_user/
Well, fine. My problem is:
When someone access a adrees with haven't a folder, .htaccess redirect it to a specific folder. For example:
I haven't a folder called "sd_joecompany", then I want to redirect it to a default folder named "sd_system". If I access http://joecompany.domain.com.br/ and not exists the respective folder, this address may point to "sd_system" keeping the address.
With this code, when I access a subdomain without a respective folder, the following error appear:
Proxy Error
The proxy server received an invalid response from an upstream server.
The proxy server could not handle the request GET /index.php.
Reason: Max-Forwards has reached zero - proxy loop?
I'm not sure about the clarity of my doubt, and I'm sorry for my poor english.
I know a little about .htaccess and these rules. Everithing on the code above, I gathered here!
Thank you since now!
If they are on the same server and have the same document root, then you really don't want to proxy at all. Remove the host part of the rule and get rid of the "P":
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com\.br$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sd_%1/$1 [L,NC,QSA]
RewriteCond %{REQUEST_URI} !^/sd_
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.domain\.com\.br$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sd_%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sd_%1/$1 [L,NC,QSA]
#Rewrite for my Framework
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1
Cleaned up some of the regex and got rid of the extra RewriteEngine On's.

htaccess exception for subdomain

I have this line in my htaccess file: RewriteRule (.*) index.php
This catches all requests being sent to my site domain.com
Now i have created a subdomain blog.domain.com and i am getting an Internal server error. How can i add blog.domain.com as an exception in htaccess.
This is my whole htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} .*/http-bind
RewriteRule (.*) /http-bind [L]
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} !/ow_updates/index.php
RewriteCond %{REQUEST_URI} !/ow_updates/
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.raw|/[^.]*)$ [NC]
RewriteCond %{REQUEST_URI} !/ow_updates/
RewriteCond %{REQUEST_URI} !/google3ce18567a119af4.html
RewriteRule (.*) index.php
Add the following line to the top of your RewriteCond block.
RewriteCond %{HTTP_HOST} !^blog\.domain\.com
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} !/ow_updates/index.php
RewriteCond %{REQUEST_URI} !/ow_updates/
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.raw|/[^.]*)$ [NC]
RewriteCond %{REQUEST_URI} !/ow_updates/
RewriteCond %{REQUEST_URI} !/google3ce18567a119af4.html
This will only pass when the request is not for blog.domain.com and meets all of your current conditions.
As a side, I would encourage you to do something more like the following as opposed to your specific file checks.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
This ensures that the request is not an existing file or directory. It's easier to maintain than whitelisting specific resources (e.g. google3ce18567a119af4.html, /ow_updates/, etc).

Resources