Change root to public inside sub-folder - .htaccess

I work in an agency and I created a subdomain in order to show our customers some code examples we can provide, all my applications are made in Laravel 5.1, and each application is being placed in a sub-directory, like this:
scripts.domain.com
/image-filter
/public
/facebook-image-app
/public
/twitter-integration
/public
/instagram-wall
/public
I need to point the root into the public folder...
I've tried this, but it didn't work as expected:
RewriteEngine on
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{HTTP_HOST} ^scripts.domain.com/image-filter$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.scripts.domain.com/image-filter$
RewriteRule (.*) /public/$1 [L]
Any idea on this?

You can try this .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{HTTP_HOST} scripts.domain.com$
RewriteRule ^([^\/]+)/?(.*)$ $1/public/$2 [L]

Related

.htaccess redirect from subfolder to domain not working properly

I am using PHP, Laravel 8 Framework, and cPanel for my website. What I am trying to do is something like this.
Redirecting Routes from
http://example.com/example.com/public/
http://example.com/example.com/public/about
http://example.com/example.com/public/contact
.
.
// more
to
https://example.com/
https://example.com/about
https://example.com/contact
.
.
// more
I have the .htaccess file in the domain root public_html folder.
.htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /example.com/public/$1 [L]
Please guide me if I had done something wrong.
Please try following rules, with your shown attempts. Please make sure to clear your browser cache before testing your URLs. Make sure 2 things. 1st- Keep your htaccess rules file in same level where you have folder example.com and 2nd- keep your index.php inside /root/example.com/public folder.
Options -Indexes -MultiViews
RewriteEngine on
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/example\.com/public [NC]
RewriteRule ^(.*)/?$ example.com/public/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ example.com/public/index.php [L]

.htaccess Change root directory with exceptions for a list of directories

Hi,
I have my .htaccess code that changes the root directory from public_html to a new-site folder and it's working perfectly.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com.au$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com.au$
RewriteCond %{REQUEST_URI} !new-site/
RewriteRule (.*) /new-site/$1 [L]
the issue with it is: I want to keep a few inner pages from the old site loading from the default directory.
https://example.com.au/load-this-from-the-old-site/
https://example.com.au/load-this-from-the-old-site/another-page/
https://example.com.au/etc/
The above pages are not directories they are pages requested from the old public_html/index.php file
Any help would be appricated
Insert an exclusion condition in your rule like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com\.au$ [NC]
RewriteCond %{REQUEST_URI} !^/(new-site|load-this-from-the-old-site|etc)/ [NC]
RewriteRule (.*) new-site/$1 [L]

htaccess rewrite subdirectory as root and also redirect if the subdirectory is accessed directly

I have URLs like this
https://example.com/public
https://example.com/public/faq.php
I want to rewrite it like this
https://example.com
https://example.com/faq.php
Rule I am using is this
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^$ public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/$1 [L]
This rule works very well but it doesn't redirect if someone access "https://example.com/public/" or "https://example.com/public/faq.php"
Solution
I have found a workaround for this. I have added below code to redirect if the public directory will be accessed directly.
RewriteCond %{THE_REQUEST} /public [NC]
RewriteRule .* / [L,R]

Two rewrite rules in the same htaccess

In the www root folder of my web server I have 2 folders for 2 differents websites
www/firstsite
www/secondsite
To reach the index.php file of the first site I go to www.firstsite.com/firstProject
To reach the index.php file of the second website I go to www.secondsite.com/secondProject
I would like to put an unique htaccess file in the root folder "www". This htaccess file would have a rewriterule for both webstie.
So users would see www.firstsite.com instead of www.firstsite.com/firstProject
and www.secondsite.com instead of www.secondsite.com/secondProject
Edit: I use this htaccess to rewrite the url of www.firstsite.com(don't forget this website is located in folder www/firstsite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ firstsite/$1 [QSA,L]
</IfModule>
Edite: here is the answer thank you to hjpotter92 !
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?firstsite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/firstProject [NC]
RewriteRule ^(.*)$ /firstProject/$1 [L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?secondsite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/secondProject [NC]
RewriteRule ^(.*)$ /secondProject/$1 [L]
Use the %{HTTP_HOST} variable to test your domain name, and redirect based on that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?firstsite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/firstProject [NC]
RewriteRule ^(.*)$ /firstProject/$1 [L]
similar rules will go for your second domain.

Laravel 5 shared host subdomain htaccess

Firstly because im on a shared host I cannot place contents of my public folder directly into root. I have to place the entire project into the public_html directory and use a .htaccess to point to the public folder like below (which works fine for a single Laravel project):
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
I now created a subdomain with the host and it created a directory inside my public_html directory called store.domain.com. This will be for an entirely different Laravel project. If I place the regular folder structure with the .htaccess file into this subdomain folder I get a 503 error when accessing the subdomain. I spoke to the host and when they suggested removing the root .htaccess file the subdomain started working but the main domain fell over.
Ive been struggling for days now reading up various solutions to other peoples similar issues on WordPress but cannot get anything to work correctly. The solution below sort-of works but it keeps adding on the public folder to my URL for the subdomain (root domain project is working fine):
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^store.mc.com
RewriteRule ^(.*)$ /public/$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^store.mc.com$
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ /public/$1 [L]
Anyone had this issue or know how to solve it?
I dont really understand why this works but the below fixed my issue:
RewriteBase /
RewriteCond %{HTTP_HOST} !^subdomain
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{HTTP_HOST} ^subdomain
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/ [L]
I uploaded my website files to a subdomain, and I always see this error
You don't have permission to access this resource. Additionally, a 403
Forbidden error was encountered while trying to use an ErrorDocument to handle the request.
Just create a file .htaccess In the subdomain folder
Add this to it and save .
RewriteBase /
RewriteCond %{HTTP_HOST} !^subdomain
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
RewriteCond %{HTTP_HOST} ^subdomain
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/ [L]
Your RewriteCond is invalid without the leading slash.
change :
RewriteCond %{REQUEST_URI} !^public
to
RewriteCond %{REQUEST_URI} !^/public

Resources