Laravel 5 shared host subdomain htaccess - .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

Related

.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]

Redirecting files from a directory to subdirectory causes infinite loop

Looked on here at all the answers regarding this issue but none fixes my issue. I have a directory called pdfs which used to contain all my .pdf files. They are now all inside pdfs/sales so trying to create a redirect for all files in the pdfs directory to look inside the pdfs/sales directory instead using a .htaccess file in the root of my site. Everything I've tried so far results in an infinite loop. Here's what my .htaccess looks like so far:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^pdfs/(.*)$ /pdfs/sales/$1 [R,NC,L]
RewriteRule ^(.*)$ /index.php?/$1 [L]
The first rule redirects all www. traffic to non-www. url. The second rule is my pdfs rule. The last rule is for redirecting all requests to index.php for seo friendly urls. Is there a conflict here? Where am I going wrong?
Thanks
You can keep your rule like this (my comment inline):
DirectoryIndex index.php
RewriteEngine on
# remove www from domain name
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# redirect /pdfs/abc.pdf to /pdfs/sales/abc.pdf
RewriteRule ^pdfs/((?!(?:sales|rent)/).*)$ /pdfs/sales/$1 [R=302,NC,L]
# for all non-files and no-directories route to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?/$1 [L]
Make sure to test it after clearing your browser cache.

htaccess rewrite rules for multiple domains (symfony 2 in subfolders)

straight to the point.
I have a web hosting with 2 domains pointing to the root directory of the hosting.
I have 2 seperate projects in the root directory both symfony2
I need to redirect the domains to the seperate directories using .htaccess.
My current .htaccess file in the root folder
RewriteEngine On
#www to non www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301, L]
#domain1
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/domain1/web
RewriteRule ^(.*) /domain1/web$1 [L]
#domain2
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/domain2/web
RewriteRule ^(.*)$ /domain2/web$1 [L]
now when I type in domain1 in the adress bar. The project works correctly. What I get in the adress bar is: "domain1.com/domain1/web" And I'd like to get rid of the domain1/web part so that the functionality stays. Do I somehow use rewrite base? Or is there an other way for me to get my result? please help. Note that I'm not really used to working with .htaccess so please be gentle :)

.htaccess not working on codeigniter subdomain & directory install

I am trying to install CI under a directory which is also on a subdomain. So my base url would look like http://subdomain.domain.com/ci/
The following htaccess works fine on root of domain but not on directory, the browser says there is a redirect loop error.
Here is my code:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^sub\.domain\.com/ci/$
RewriteRule ^(.*)$ http://sub.domain.com/ci/$1 [L,R=301]
RewriteCond $1 !^(index\.php|assets)
RewriteRule ^(.*)$ /index.php?/$1 [L]
I am not very skilled with mod rewrite, any help would be appreciated! Thank you!
Inside your /ci directory your htaccess should be as follows
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f # this means if the requested file name doesnt exist
RewriteCond %{REQUEST_FILENAME} !-d # this means if the requested directory doesnt exist
RewriteRule ^(.*)$ index.php [L]
Based on your above comments that should be sufficient

redirect a domain to a certain folder (Joomla Site)

I have a website hosted in cpanel. Files of this website are in a folder. In the panel there is no possibility to host a domain to this folder, so when I go to www.mydomain.com I can see all folders. I would like to redirect my registered domain to a folder where my website is.
I use htaccess file and try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteRule ^(.*)$ www.hostdomain.com/folder/$1 [L]
but it doesn't work.
This:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteRule ^(.*)$ folder/$1 [L]
and still nothing.
What I do wrong? I would be grateful for some advice.
Edit: I dealt with it.
I use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/folder/
RewriteRule ^(.*)$ /folder/$1 [L]
then in order to hide joomla folder I add to joomla htaccess:
RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) http://www.mydomain.com/$1/ [R=301,L]
RewriteCond %{REQUEST_URI} !^/folder
RewriteRule ^(.*)$ folder/$1 [L]
and next I change configuration.php file in joomla folder:
var $live_site = 'http://www.mydomain.com/';
It works.
Thanks a lot for your answers.
test it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mydomain.com$
RewriteCond %{REQUEST_URI} !^/path/to/folder/
RewriteRule ^(.*)$ path/to/folder/$1 [L]

Resources