Hide Subfolder in URL with .htaccess without breaking other subdomains - .htaccess

I am trying to keep my main domain structure from being too cluttered so I am parsing all of my domains into their own subfolder. So, what I am trying to do is when a user goes to http://mydomain.com they are actually sent to http://mydomain.com/sub-directory
This bit of code works:
#redirect to submain subdomain
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /submain/
RewriteRule ^submain/(.*) /$1 [L,R=301]
RewriteRule !^submain/ submain%{REQUEST_URI} [L]
However it breaks all the other subdomains I have loaded into my main directory.
Any ideas on how to fix this?

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /submain/
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$
RewriteRule ^submain/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain.com$
RewriteRule !^submain/ submain%{REQUEST_URI} [L]

Create a .htaccess file in root folder, and put this content inside(just change example.com and my_subdir):
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/my_subdir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /my_subdir/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ my_subdir/index.php [L]
</IfModule>

Related

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]

Htaccess rewriterule, file was not found on this server

I am new to the whole htaccess language, but I feel like I am close to what I want.
Facts:
parts is a real folder/directory
anotherpart is a real folder/directory
there are no other files in <root> other then index.php
there are no files in the parts folder/directory
Folder/Directory structure:
<root>/index.php
<root>/parts/anotherpart/this.php
What .htacces I have running:
RewriteEngine On
RewriteBase /
RewriteRule ^parts/anotherpart/(.*)$ /$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.somewebby.it$ [NC]
RewriteRule ^(.*)$ https://somewebby.it/$1 [R=301,L]
Results:
always having https and no www
hiding parts/anotherpart and showing https://somewebby.it/this.php
The problem:
The requested URL /this.php was not found on this server.
You can replace your current code by this one in your htaccess (which has to be in root folder)
RewriteEngine On
RewriteBase /
# if "www" or http -> redirect to https://domain.com/xxx
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://somewebby.it/$1 [R=301,L]
# hide "parts/anotherpart/"
RewriteCond %{THE_REQUEST} \s/parts/anotherpart/([^\s]+)\s [NC]
RewriteRule ^ %1 [R=301,L]
# silently rewrite back to "parts/anotherpart/"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/parts/anotherpart/$1 -f
RewriteRule ^(.+)$ parts/anotherpart/$1 [L,QSA]

Redirect some pages to HTTPS

First I directed my /example folder to main domain. Now, I'd like to redirect some of the pages to HTTPS. Pls, anyone could advice me about the code:
# com upload yonlendirme
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteCond %{REQUEST_URI} !^/example/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example/$1
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ example/index.php [L]
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
#redirect www.mydomain.com to mydomain.com (or any other subdomain)
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
#force https for certain pages
RewriteCond %{HTTPS} !=on
RewriteRule ^(index\.php?route=account/register|index\.php?route=account/login|index\.php?route=account/account|index\.php?route=checkout/checkout)$ https://%{HTTP_HOST}%{REQUEST_URI}[L,R]
You can't match against the query string (everything after the ?) in a rewrite rule. You'll need to match against the %{QUERY_STRING} variable in a rewrite condition:
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} ^route=(account/register|account/login|account/account|checkout/checkout)
RewriteRule ^index.php$ https://%{HTTP_HOST}%{REQUEST_URI}[L,R]
You may also want to move that rule up to the top of your htaccess file so the routing to the example folder doesn't interfere with the redirects.

Force HTTPS on rewritten url via htaccess

I have a website on www.domain.com and here is the root (and only) .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico)$ [NC]
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I'm basically doing rewrite on my URL's so everything is going through the index.php page.
How can I force HTTPS (SSL) on www.domain.com/login, /register and /settings? So when I try to access the http://www.domain.com/login I should be immediately redirected to https://www.domain.com/login?
I searched SO and I found for folders so if I have folder /login it would work, but not for me in this case of already rewritten URLs.
Using mod_rewrite you should be able to do something like this
RewriteCond %{SERVER_PORT} !443
RewriteCond %{REQUEST_URI} ^/register
RewriteRule (.*) https://www.domain.com/register [R]
Try:
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^login$ https://www.domain.com/login [R=301,L]
RewriteRule ^register$ https://www.domain.com/register [R=301,L]
RewriteRule ^settings$ https://www.domain.com/settings [R=301,L]

htaccess server error when removing www from url

.htacess error
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.netsentries.com
RewriteRule ^(.*)$ http://netsentries/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Error only comes when I add this line.
RewriteCond %{HTTP_HOST} ^www.netsentries.com
RewriteRule ^(.*)$ http://netsentries/$1 [R=301,L]
I have to add it because my site is not loading with www now. I want to make it load.
Use this code and your htaccess problem will be solved:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# no www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
and if you have problem loading your site with www it is because of loosing www CNAME record from your website's host. You should check the nameservers from "DNS zone editor" of your websites hosting package and adding required records for www like this:
NAME:www.yoursite.com. TTL:14400 TYPE:CNAME RECORD:yoursite.com.
this will cause your website to work in both with and without www.
Try this once
RewriteCond %{HTTP_HOST} ^www\.netsentries\.com
RewriteRule ^(.*)$ http://netsentries.com/$1 [R=301,L]

Resources