I have a laravel 5 installation. Works perfect.
.htaccess inside the public folder is default and works fine:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
Inside the public (root) folder i have a folder named webapp
webapp includes a web version of my app build with angularjs and it needs push states
the ideal .htaccess inside the webapp folder would be:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# html5 pushstate (history) support:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
</ifModule>
But it seems the .htaccess inside the webapp folder is not triggered.
I probably need to write the conditions and rules inside the laravel public folder. But i'm not sure how to handle this and in what order etc.
Can someone help me to combine the 2 .htaccess files into one to place inside the public folder.
This works for me:
bash-3.2$ cat .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301] #This creates a redirect loop for angular
# IF the resource does not exist as an actual directory...
RewriteCond %{REQUEST_FILENAME} !-d
# OR an actual file...
RewriteCond %{REQUEST_FILENAME} !-f
# AND is being requested from angular directory a/
# THEN send it to the angular routing file
RewriteRule ^(a/.*) /a/index.html [NC,L]
# IF the resource does not exist as an actual directory...
RewriteCond %{REQUEST_FILENAME} !-d
# OR an actual file...
RewriteCond %{REQUEST_FILENAME} !-f
# THEN send the traffic to laravel routing
RewriteRule ^ index.php [L]
</IfModule>
bash-3.2$
Related
I have a React app with the react router.
After build I try to start react app on my hosting server.
App is working in root directory /example.com correctly.
But I want to run it at subdirectory like /example.com/sub and it doesn't work.
It's possible, problem in .htaccess file.
Here's .htaccess content
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
I decided to change my react-app files to be at the subfolder public_html/main/ --for git automation reasons--, then I also made some changes in my .htaccess file so it can read the main folder.
Now, reading the main/index.html is working fine, although the .htaccess is not detecting static/ inside main, instead is looking for it in public_html/ (I know it because when I put static there it worked). And I need to change in some way this dot file to read static in the subfolder.
I have the next folder tree structure at my server:
public_html/
.htaccess
main/
index.html
static/
css/
js/
media/
While .htaccess looks like:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
RewriteCond %{HTTP_HOST} ^(www.)?udecursos.study$
RewriteCond %{REQUEST_URI} !^/main/
RewriteRule ^(.*)$ /main/$1
RewriteCond %{HTTP_HOST} ^(www.)?udecursos.study$
</IfModule>
I managed to solve it, when I knew that you can have a .htaccess file in both folders, the root and the sub one. When the dot file in the subfolder will override the others .htaccess.
So I configured the root (public_html/) .htaccess like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?udecursos.study$
RewriteCond %{REQUEST_URI} !^/main/
RewriteRule ^(.*)$ /main/$1
RewriteCond %{HTTP_HOST} ^(www.)?udecursos.study$
While in the subfolder with location public_html/main/ the .htaccess ended
up like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
Actually working and reading the needed files.
I'm deploying my first project on Hostinger. It's a React app created according to the basic config given on reactjs.org. I have uploaded my entire folder to the server via FileZilla and am now trying to configure my .htaccess file but I am only getting 500 errors back. I put this file in my folder called react-portfolio but nothing works. The whole project is stored in the react-porfolio folder.
here is what I tested first
DirectoryIndex /react-portfolio/public/index.html
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ react-portfolio/public/$1 [L]
</IfModule>
here is the second file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
Thank you
I have deployed two react builds on cpanel, basically one is admin panel and second website like this:
admin folder consist of build of admin panel working on /admin route.
client folder consist of build of website working on / route.
My .htaccess Code:
<IfModule mod_rewrite.c>
RewriteEngine on
# first check if request is in /client/
RewriteCond %{DOCUMENT_ROOT}/client%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/client%{REQUEST_URI} -d
RewriteRule ^(.*)$ /client/$1 [L]
# then check if request is in /admin/
RewriteCond %{DOCUMENT_ROOT}/admin%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/admin%{REQUEST_URI} -d
RewriteRule ^(.*)$ /admin/$1 [L]
</IfModule>
After long searching I found this code as solution for my app as two react apps are deployed in cpanel. The problem is that whenever I refresh on any route page gives 404 error although routes work perfect. Please Help.
I'm using these rules for my react applications, in my cases I have one react application not two
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
I've got an Angular app using html5mode, but it's currently sitting within a sub-folder of a site: http://somesite.com/za/
So I've managed to rewrite the url to direct to my assets folder correctly, but I'm still getting 404s when I hit refresh or navigate directly to a page.
So this works: http://somesite.com/za/assets/js/bundle.js
This doesn't work, 404: http://somesite.com/za/home
.htaccess
RewriteEngine On
#This apparently creates a redirect loop for angular
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(za/.*) /za/index.html [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?:[^/]*/)*([^/.]+\.(?:jpe?g|gif|bmp|png||css|js))$ za/assets/$1 [R=301,L,NC]
AddType image/svg+xml svg svgz
AddEncoding gzip svgz
So where is the rewrite failing to redirect to index.html when I hit or refresh a page?
Solved it with a more elegant rewrite rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /za/index.html [NC,L]
However, this does give read access to other files and directories, so it can have security issues.