Preact app using react router doesn't render routes in sub folder - .htaccess

I have a Preact app using react router, and trying to run it from a sub folder on my shared hosting provider.
When I load the site, neither the first route or any subsequent routes load. Only the main page and navigation load (and change the url on click).
I tried using basename="/company" on <Router basename="/company"/>, but this doesn't seem to make a difference
I also created a .htaccess file with the following:
<IFModule mod_rewrite.c>
RewriteEngine On
RewriteBase /company
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IFModule>
But can't get the routes to load. I see in the console that they're trying to load from the root of the server (domain.com/route-Colors.chunk.123abc.js), rather than the folder they're in (domain.com/company/route-Colors.chunk.123abc.js).
What am I missing in order to allow Preact and react router to load the initial view and all subsequent routes from a sub folder on my shared hosting provider?

I'm assuming this is a preact-cli app, though you haven't said.
This is what Webpack's publicPath does. You'll need to configure it in your config file.
preact.config.js
export default (config, env, helpers) => {
if (env.production) {
config.output.publicPath = 'https://example.com/company';
}
}
Normally the public path is /, so it tries to load all assets from the root directory.

Related

Laravel project in subfolder of public_html gives trouble with .htaccess redirect

I've set up my VPS server and am trying to set up a Laravel project.
I have a /public_html/ folder with an index.html that's shown when visiting the website URL.
I have a laravel project in: /public_html/project_one/
The original index.php is located in /public_html/project_one/public/index.php
I want the website to show to Laravel project_one by default, but since yesterday I literally googled this for 4 hours, I tried every solution on stackoverflow, nothing worked... I think I've tried over 10 versions of .htaccess I could find.
The question:
Does anyone know how to setup the .htaccess inside /public_html/ so it redirects to my Laravel project?
I though I needed to upload my Laravel folder in: /public_html/project_one/ but is there a better location?
I want the main url to show the Laravel project. So www.mainurl.com is Laravel's project_one/public/index.php file. I do not want /project_one/ to be written in the URL!
Just out of curiosity, if I now visit: www.mainurl.com/project_one I get a 403 forbidden.
Failed solutions
In /public_html/ I tried to add .htaccess with:
DirectoryIndex index.php
#Redirect to /project_one/public if you haven't already and the IP is okay
RewriteCond %{REMOTE_ADDR} ^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} !^/project_one/public
RewriteRule ^(/lucaphoto)?(.+) /project_one/public$2 [L]
#if IP does not match and you ARE in the folder, then Redirect to root
RewriteCond %{REMOTE_ADDR} !^1\.2\.3\.4*
RewriteCond %{REQUEST_URI} ^/project_one/public
RewriteRule .? / [R=301,L]
This gives me something really strange:
On Chrome: it redirects me to www.mainurl.com/project_one -> And then gives me a 403 forbidden error.**
On safari: it redirects me nowhere but gives me this error:
Forbidden. You don't have permission to access / on this server.
Additionally, a 404 Not Found error was encountered while trying to
use an ErrorDocument to handle the request.
Laravel's original .htaccess File
By the way, Laravel has an original .htaccess file at /project_one/public/.htaccess. And it looks like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
The actual laravel application ( anything but the public folder ) is not supposed to be public and therefore should not be placed inside the public ( public_html ) folder.
I actually tried severel things to get something similar to your setup running. My conclusion was - that only changing the .htaccess file will net e enough to run laravel inside a public folder.
The most easy solution to quickly setup a laravel application is to use a subdomain which directly points to the public folder.
www.mainurl.com /* points to .../public_html/ */
laravel.mainurl.com /* points to .../laravel/public/ OR ( but not good ) */
/* points to .../public_html/my-project/public/ */
What you actually will need to do then is to change the root of your domain / subdomain to point into another folder - no changes to .htaccess are needed then.
Update
If your site is www.example.com and you want your laravel app to show when opening this site you have two options.
Put the content of laravels public folder to public html and change the references to fit the public_html instead of public folder.
If your Filestructure on the server is like .../var/stuff/.../public_html/ then upload everything to ../var/stuff/my-project and then go to your hosters interface and make an internal redirect to the ../var/stuff/my-project/public folder and everything will be fine without chaning anything in your laravel code. ( anything but .env )
So the actual problem is that your domain points to the folder public_html, instead of the public folder of your app. However please dont put your app into public_html directly, put it parallell to it and change where the domain points to.
if you give us some information about your hoster we might can provide a guide

How to separate same directory - controller name in Codigniter

I've got a controller called course and a directory with the same name, and when I request
my_site/course
it redirects my to the directory course Instead of controller, how can I redirect to the controller
P.S It's very necessary to keep directory with the same name in my apllication
Does the directory need to be int he root folder, can it not be further down the hierarchy? By having a directory with the same name as your controller in the root directory, your web server is serving up the directory instead of normal CodeIgniter routing.
If it's an absolute must to have the folder in the root, then you would need to change your .htaccess file to ignore requests which match the directory name for that directory (and any others which match controller names).
There's an answer for this already, as well as a good example on the Drupal website which I've put in below in case it gets moved
=========[ start of .htaccess snippet]==========
<IfModule mod_rewrite.c>
RewriteEngine on
#
RewriteCond %{REQUEST_URI} !^/yourDirectoryName
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]
</IfModule>
====================[ end ]=====================
There nativ Ci routing.
In config/routes.php
$route['course'] = 'controller/course';
after default routes

Laravel Routing issue for fetch images

I am doing Upload gallary in laravel. After completion of uploading, I am trying to display Images on browser. Here the problem is, I uploaded images on UPLOADS Directory outside of (parllel) PUBLIC folder. While fetching that image, the path is wesbite.com/Uploads/xxx.jpg . here Uploads treat as Routing path. So I need to ignore routing for that. Can anybody suggest please.
I tried with .htaccess RewriteRule ^(uploads) - [L] . But it completley ignores the Uploads directory.
The public folder is the webroot, anything outside of it cannot be accessed via the browser. If you want the images to be accessed to be accessed through something like /uploads/xxx.jpg you'll need a directory called uploads within the public directory.
Unfortunately that's the way that it works, you can view things on the web that are outside the webroot, although you could do something like, having a controller and method that uses something like file_get_contents() to load it.
I had a case before that I was having a redirect loop problem because one of my routes is the same as the name of a directory in public. Edit your public/.htaccess
# Redirect trailing slashes but only if the directory is non existent
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [L,R=301]
# Handle front controller but only if file is non existent
# RewriteCond %{REQUEST_FILENAME} !-d (comment this out otherwise the url will route to the directory content listing not your preferred controller action)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Yii and .htaccess file on production server

I am new to yii and have some problem in configuring .htaccess file on production server.
On localhost :
Location of Application : /www/connect_donors/
Default URL that yii provides is,
http://localhost/connect_donors/index.php?r=controllerId/functionName
We used the urlManager in /connect_donors/protected/config/main.php to configure the SEO friendly url's..
'<controller:\w+>/<id:\d+>' => '<controller>/view',
'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
'<controller:\w+>/<action:\w+>' => '<controller>/<action>'
Now the URL that was working was
http://localhost/connect_donors/index.php/controllerId/functionName
Then I used the .htaccess file to remove index.php from the above URL.
Location of .htaccess is : /connect_donors/.htaccess
Following is .htaccess file,
RewriteEngine On
RewriteBase /connect_donors
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
URL Chenged to :
http://localhost/connect_donors/controllerId/functionName
Everything working fine and awesome.
But yesterday I uploaded the application on production server.
On Production Server
Everything remained same only I had to change the .htaccess file.
The .htaccess file on server is,
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php/$1 [L,QSA]
Now the following url:
http://donorsconnect.com/
loads the home page of server properly.
But,
http://donorsconnect.com/profile
redirects again to home page.
NOTE : There is no session set on the "profile" controller.
class ProfileController extends CController
{
public function actionIndex()
{
$this->render('index');
}
}
I tried lot of things, changing the .htaccess file to different codes. but none helped me.
Any help is appreciable.
Solution
I finally got the solution and the mistake I had done.
My Components had a request array containing baseUrl.
'components'=>array(
...
'request' => array(
'baseUrl' => 'http://donorsconnect.com',
...),
Due to this it was not loading. I did not find the real reason for that.
But after removing that 'request' array, its loading fine.
Check link,
http://donorsconnect.com/profile
You should not hide index.php like this, read carefully : http://www.yiiframework.com/doc/guide/1.1/en/topics.url#hiding-x-23x
You should set showScriptName to false in your main config :
'urlManager'=>array(
.....
'showScriptName'=>false,
.....
),
And your .htaccess should look like this :
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

Backbone.js PushState routes .htaccess only working as hash but nowhere else

I have a website domain.com for example. I have backbone.js with pushstate and fallback and when I goto domain.com/about it loads up the index.html page and pushstates to about. everything is working. but if i want to goto a directory with a page inside like: www.domain.com/bio/moreinfo for example, it does not work and throws a invalid page. if i do it in IE it works fine. my htaccess file has the following:
RewriteEngine on
# html5 pushstate (history) support:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L]
</ifModule>
if i navigate directly to the page domain.com/bio/moreinfo it craps out (i think because my server wants to goto the bio directory? or maybe I need to actually control the routes in backbone differently? its just that it works on bangs so it has to be some weird push state directory thing where #bio/info is not the same to apache as /bio/info . any help is appreciated.
well using the answer in another suggested post, which was to do
<base href="/" />
in the index.html file. This actually made the sub directories in my pushState work! just it was suggested.. but then in return it broke my IE, but i fixed it by putting extra code in my INIT of backbone
Backbone.history.start({ pushState: Modernizr.history, silent: true });
if(!Modernizr.history) {
var rootLength = Backbone.history.options.root.length;
var fragment = window.location.pathname.substr(rootLength);
var search = window.location.search;
Backbone.history.navigate('/#' + fragment + search, { trigger: true });
} else {
Backbone.history.loadUrl(Backbone.history.getFragment())
}

Resources