.htaccess redirect to public folder, exclude subdomain - .htaccess

I have a PHP MVC website, the root needs to be redirected to public folder.
www folder includes:
- app
- public
- subdom/subdomain
subdomain folder is subdomain.website.com
app folder is restricted with Options -Indexes
I need to redirect to public folder, but not if I'm accessing subdomain.
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
This will always get me into public folder but I can't access my subdomain.
I can always either access my subdomain or be redirected to public folder but even when I want to access my subdomain.
This is default .htaccess file in the www folder on Wedos webhosting.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^domains/
RewriteCond %{REQUEST_URI} !^/domains/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$
RewriteCond %{DOCUMENT_ROOT}/domains/%2 -d
RewriteRule (.*) domains/%2/$1 [DPI]
RewriteCond %{REQUEST_URI} !^subdom/
RewriteCond %{REQUEST_URI} !^/subdom/
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)\.([^\.]*)\.([^\.]*)$
RewriteCond %{DOCUMENT_ROOT}/subdom/%2 -d
RewriteRule (.*) subdom/%2/$1 [DPI]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^domains/[^/]+/(.+[^/])$ /$1/ [R]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^subdom/[^/]+/(.+[^/])$ /$1/ [R]
How do I exclude subdomains from the redirect?
website.com should be website.com/public
subdomain.website.com should remain that way - it should not be website.com/public
Thanks!

You should add a RewriteCond before 2 rules that deal with /public to check current host name:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?website\.com$ [NC]
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?website\.com$ [NC]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

Related

Strange redirect to https://www.example.com/cs_CZ

I use this tuned htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]
RewriteRule ^\.htaccess/?$ - [NC,F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /public/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/public/.*$ [NC]
RewriteRule ^(.*)/?$ /public/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^public/.*$ /public/index.php [QSA,NC,L]
When I try a url that demonstrably does not exist, e.g. https://www.example.com/aaa, it redirect to https://www.example.com/cs_CZ and ERR_TOO_MANY_REDIRECTS error. In application is not such redirect. Could be cause it by some rule of htaccess?
In addition to redirecting to the https and www versions of the website, htaccess points from the root of the website to the public folder, where the index.php file is.

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]

HTACCESS redirect controller folder to subdomain

I am stuck at a problem with .HTACCESS FILE.
I am using CodeIgniter 3
Scenario:
We have a domain https://example.com and the admin panel is in a folder like
application/controllers/manage/<controllers here>
Now I need this to happen.
If someone goes to https://example.com/manage should be redirected to
https://admin.example.com/manage.
Keep in mind that both subdomain & main domain points to same directory.
Also I want this to happen:
If someone goes to any url like
https://admin.example.com/<anyhting-other-than-manage>
to redirect to
https://example.com/<anything-other-than-manage>
Here is my current .htaccess
# gtranslate config
RewriteRule ^(af|sq|am|ar|hy|az|eu|be|bn|bs|bg|ca|ceb|ny|zh-CN|zh-TW|co|hr|cs|da|nl|en|eo|et|tl|fi|fr|fy|gl|ka|de|el|gu|ht|ha|haw|iw|hi|hmn|hu|is|ig|id|ga|it|ja|jw|kn|kk|km|ko|ku|ky|lo|la|lv|lt|lb|mk|mg|ms|ml|mt|mi|mr|mn|my|ne|no|ps|fa|pl|pt|pa|ro|ru|sm|gd|sr|st|sn|sd|si|sk|sl|so|es|su|sw|sv|tg|ta|te|th|tr|uk|ur|uz|vi|cy|xh|yi|yo|zu)/(.*)$ /gtranslate/gtranslate.php?glang=$1&gurl=$2 [L,QSA]
RewriteRule ^(af|sq|am|ar|hy|az|eu|be|bn|bs|bg|ca|ceb|ny|zh-CN|zh-TW|co|hr|cs|da|nl|en|eo|et|tl|fi|fr|fy|gl|ka|de|el|gu|ht|ha|haw|iw|hi|hmn|hu|is|ig|id|ga|it|ja|jw|kn|kk|km|ko|ku|ky|lo|la|lv|lt|lb|mk|mg|ms|ml|mt|mi|mr|mn|my|ne|no|ps|fa|pl|pt|pa|ro|ru|sm|gd|sr|st|sn|sd|si|sk|sl|so|es|su|sw|sv|tg|ta|te|th|tr|uk|ur|uz|vi|cy|xh|yi|yo|zu)$ /$1/ [R=301,L]
RewriteCond %{HTTP_HOST} ^static.example.com [NC]
RewriteRule !^(assets|uploads)/ https://example.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^admin.example.com [NC]
RewriteRule !^(manage)/ https://example.com%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(manage)/ https://admin.example.com/manage%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# procedure was miss spelled
Redirect 301 /yellwo /yellow
# Redirect 301 ......
Please help
UPDATE: Following does seems to work for redirecting admin.example.com<NOT-MANAGE-URI> to example.com/<NOT-MANAGE-URI>
<IfModule mod_rewrite.c>
RewriteEngine On
#gtranslate code
# ........
RewriteCond %{HTTP_HOST} ^static.example.com [NC]
RewriteRule !^(assets|uploads)/ https://example.com%{REQUEST_URI} [R,L]
# REDIRECT ADMIN
RewriteCond %{HTTP_HOST} ^admin.example.com [NC]
RewriteCond %{REQUEST_URI} !^/manage/
RewriteRule (.*) https://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
.......
After this:
URL in browser https://admin.example.com/about-us redirects to https://example.com/about-us which is correct.
but writing https://admin.example.com/manage/ redirects to https://example.com/index.php/manage/
You can insert this rule at top of your .htaccess to redirect example.com/manage to admin.example.com/manage:
RewriteCond %{HTTP_HOST} ^(?:www\.)?.(example\.com)$ [NC]
RewriteRule ^manage(?:/.*)?$ https://admin.%1%{REQUEST_URI} [NC,R=301,NE,L]

Redirect in htaccess to folder and https

Using htaccess I want to force http to https and at the same time redirect to www. and to the 'public' folder where my application files are.
The result should be that http://example.com should redirect to https://www.example.com/public/
To redirect to https and www I manage by using:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
To redirect to the 'public' folder I manage by using:
ReWriteCond %{REQUEST_URI} !^public/
ReWriteRule ^(.*)$ public/$1 [L]
Now I need to combine these but my many attempts did not result in a working redirect and I would need some advise on this.
EDIT:
In the folder public is another htaccess file:
SetEnv APPLICATION_ENV "production"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
<<
ReWriteCond %{REQUEST_URI} !^public/
ReWriteRule ^(.*)$ public/$1 [L]
The REQUEST_URI server variable starts with a slash, so the condition above will never match. For example:
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule (.*) public/$1 [L]
However, assuming you have another .htaccess file in the /public subdirectory I would write this block like the following instead:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) public/$1 [L]
This has the added benefit of allowing a public path segment at the start of your URLs (if you wish). eg. The URL /public/foo would internally rewrite to /public/public/foo without creating a rewrite loop.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
You are missing an OR flag on the first condition. For example:
RewriteCond %{HTTPS} off [OR]
:
Without that, it will fail to redirect http://www.example.com/ to HTTPS.
Just to clarify, the canonical redirect should go before the internal rewrite to the public directory.
UPDATE:
EDIT: In the folder public is another htaccess file:
SetEnv APPLICATION_ENV "production"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ /index.php [NC,L]
The substitution string on the last RewriteRule directive is incorrect. You need to remove the slash prefix from /index.php. It should just read index.php. Otherwise it is going to rewrite the request back to the document root (filesystem path) and result in either an endless loop or a 404 (depending on the parent directives). By changing it to index.php (no slash) it is now relative to the /public directory (the directory that contains the .htaccess file), instead of the document root.
You can also simplify the RewriteRule pattern and the NC flag is not required here. So, in summary, the /public/.htaccess file should read:
SetEnv APPLICATION_ENV "production"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^ index.php [L]

How to write a htaccess file to directory from wildcard subdomain

I am creating dynamic (wildcard) sub-domains using .htaccess like so:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) /index.php?n=%1&v=$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com/why/$ [NC]
RewriteRule ^(.+)$ /$1/ [QSA,L]
When a directory is included in the URI, e.g. dynamic.domain.com/directory the URL changes to the "real" subdomain like so: subdomain.domain.com/directory/
My question is how the keep the URL from showing the actual subdomain.
xyz.domain.com works as expected
xyz.domain.com/dir will change the URL to dynamic.domain.com/dir which should be xyz.domain.com/dir

Resources