Redirection issue on Symfony 5 - .htaccess

I try to publish a symfony application on an web server.
The structure of the webserver is :
www
gpexe
config
public
src
var
templates
tests
vendor
I've setup an htaccess on the root folder www:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/gpexe/public/
RewriteRule ^(.*)$ /gpexe/public/$1 [L]
And there's also an htaccess on the public folder generated by Symfony avaliable here.
I managed to access to the main page when typing the root address (www.gpexe.fr), but the css and js link are redirected correctly, for instance :
No route found for "GET /build/login.css"
This because the address /build/login.css must be replaced by /gpexe/public/build/login.css to be usable.
I also try to validate the root htaccess on this website and the address /build/login.css is well rewrited by /gpexe/public/build/login.css.
What am I doing wrong ?

Related

Accessing specific URL throws 404 error for website hosted on GoDaddy

I have a angular7/mysql/node.js website hosted on GoDaddy and I'm facing following issue.
When I access website using base URL like www.abcdef.com, it opens home page and then using links provided on home page to go to other pages like About Us, Contact Us, etc. I can access those pages.
But if I try to access a page using specific URL like www.abcdef.com/aboutus, website throws an 404 error and I'm redirected to error page.
I'm new to hosting and stuff so any help would be greatly appreciated.
Thanks
Have you configured your server to redirect requests to missing files to index.html?
See https://angular.io/guide/deployment#server-configuration. I'm not familiar with GoDaddy hosting, but I'm guessing you should add the .htaccess file (create/edit the .htaccess file in the root folder):
RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

Is it possible to run node app alongside prestashop instance on the same VM?

I want to run nodeJS app which sole purpose is just to make some redirects based on DB queries, the app resides on main domain let's say example.com.
On a subdomain shop.example.com there is a prestashop 1.7 installed.
The virtual server software is cPanel.
Prestashop instance works as long as I don't install the node app, when I do it seems to make both example.com and shop.example.com unresponsive, and after a while it returns 503. The node app runs on port 8080.
You can try to add a .htaccess file that will redirect requests to the nodejs app. So inside your public_html, create a file named ".htaccess" and put this in it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^api/(.*)?$ http://127.0.0.1:8080/$1 [P]
</IfModule>
And you should make request to this link instead : http://example.com/api/aS7vXbSDn9
The htaccess will redirect any request which contains /api to the nodejs app and pass whatever comes after the api/ to the app as a url paremeter.
For more information about htaccess, you can refer to this link
You can add these two lines as well (Before RewriteRule):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
This will make sure you only redirect request made for the api, not requests made to access files or directories.

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

Symfony .htaccess

I configured a test folder for websites in hostgator, so far I have no problems in some flatform like wordpress,magento,joomla, I did not even change any seetings in .htaccess to make it run. Recently, I am working on a symfony2 project and upload my test application in the test folder I created. This is how I create a test folder
mywebsite.com/tests/wordpress_site(no problem)
mywebsite.com/tests/joomla_site(no problem)
mywebsite.com/tests/symfony2_site(403 forbidden error)
In my WAMP, I can accessed my symfony project
localhost/symfony_site/web/
I did not touch the .htaccess since this is just a test site in local machine.
How would you configure a symfony2 project in this setup?
mywebsite.com/tests/symfony_site
It is almost impossible to create clean Urls when installing symfony in subfolder...
With Apache, the better solution is to use a VirtualHost which have tests/symfony_site/web as DocumentRoot, and to use the built-in .htaccess file (which uses url rewriting module, or if it not available, uses a 302 redirection to app.php/)
But If you can't add a virtualHost to your server, it is hard...
An other way is to add a .htaccess at tests/symfony_site and put DirectoryIndex: web/app.php
(https://httpd.apache.org/docs/current/mod/mod_dir.html)
So you can have clean URLs... but be careful with this solution: the urls parsed by the router are relative to your site root (and not to symfony root...) so the router might be lost... (e.g if you have a root named /test you should rename it /tests/symfony_site/test... And it is the same for assets, you have to add the site directory, otherwise they will return a 404 error...
In my case, I just use existing .htaccess in web folder (or I create a new one with a DirectoryIndex Instead), so my app work with URLs like mywebsite.com/tests/symfony_site/web/
I hope this helped,
Vincent
You can try typing the following code into the .htaccess file to the symfony root folder
RewriteEngine On
RewriteBase /
RewriteCond $1 !^(img|files|images|favicon.ico|css|js|robots\.txt)
RewriteRule ^(.*)$ web/$1 [L]
RewriteCond %{HTTP_HOST} host.com
RewriteCond %{REQUEST_URI} !web/
RewriteRule ^(.*)$ web/$1 [L]

.htaccess redirect dynamic domain to folder

For local development I've setup my router to redirect all requests to *.lan to my local xampp server. Now I have to setup virtual hosts for every domain I want to use. This works, but I don't want to setup a new virtual host (pointing to the corresponding folder) for every project and restart apache.
Is it possible to create a .htaccess file to "catch" a domain, and rewrite it to the folder, so I can use the domain as the folder name? For example:
example1.lan loads the website located in /example1/
example2.lan loads the website located in /example2/
This tread ( Apache - Domain for localhost to access folders as http://folder.local ) does almost the same thing, but with a redirect. I don't want a redirect, I want rewrite.
Thanks in advance!
Try adding these rules to the htaccess file in your document root (the directory where the example1 and example2 directories are):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.lan$
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^(.*)$ /%1/$1 [L]

Resources