Run multiple subfolder websites in one separate folder - php

I have a main website (example.com). I use CodeIgniter framework there.
I will also use some different subfolders like example.com/sub1, example.com/sub2. I am using Laravel framework here and those will be completely different from example.com and stored in a different folder. When I started first subdomain, I used Alias in Apache2 configuration and it was working perfectly well. However I now need to use a second subfolder. I added a second Alias sub2 in Apache configuration that points to the same directory as sub1. Then I changed my .htaccess to this:
<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
RewriteCond $1 !^(img|js|css)
RewriteCond %{REQUEST_URI} (/evim)
RewriteRule ^ /evim/index.php [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 !^(img|js|css)
RewriteCond %{REQUEST_URI} (/crm2)
RewriteRule ^ /crm2/index.php [L]
</IfModule>
Now it works fine. Both example.com/sub1 and example.com/sub2 runs the same application. However in Laravel, it is hard to find which subdomain is running currently. In routes, the application does not see subfolder name. For example if I use users/login route, then it matches both example.com/sub1/users/login and example.com/sub2/users/login. I need to define routes with subfolder name.
I am looking for a solution in htaccess or Laravel to fix the routes.

I would use this pattern (as sub domains) and it may perhaps avoid problems.
http://site1.foo.com http://site2.foo.com
In CodeIgniter Route.php you can do the following:
$server_link = explode(".", $_SERVER["HTTP_HOST"]);
if ( $server_link[0] == "site1" )
{
$route['default_controller'] = 'home';
// Some Other Routes
}
I think you can do the same thing for Laravel routes.

I used Request::server('HTTP_HOST') to get which site is loaded.

Related

Laravel 8.4 – Remove Public from URL

I am working on creating a website that converts text into speech. I am using the script from codecanyon which is Developed with PHP 7.4.x and Laravel 8.4.x . The issue I am facing is that I don't want to show /public URL as a permalink to access the website. Now, if I type mywebsite.com/pubic then the website is accessible otherwise if I write mywebsite.com then it only shows the files. I followed some of the previous queries posted on StackOverflow but got no results.
The topic I followed is Laravel 5 – Remove Public from URL
I tried even by renaming server.php , copying .htaccess and rewriting it. But those all resulted in a screen showing up errors or even blank.
My .htaccess file is;
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
To remove the /public from the URL you are not going to use the .htaccess file.
What you are going to do instead is set the /public folder as the serving directory for your Server.
If you use Apache, just go to your /etc/apache2/sites-available/000-default.conf file and change the DocumentRoot from {your base folder}/ (usually /var/www) to {your base folder}/public
The content of public folder should go to the public folder of your server (fe it's public_html on my server), and everything else should be stored in a different folder (fe laravel_mywebsite). Then you should go to public_html/index.php and change the folder paths based on where you put your Laravel app.
If you want to only use .htacces, you just need to rewrite all requests from /public to /
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,R=301]

pretty urls laravel 5.4

Hi I am facing issues with settng pretty urls withput public in url but it is not working for me.
I already tried
Laravel 5.2 Pretty URLs,
Laravel 5 - Remove public from URL
and more similar urls but nothing seems to work for me.
I am using wamp, windows 8
I have make sure mod_rewrite is on, and is working properly.
Here is my htaccess on root.
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
my htaccess in test inside public folder
<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>
I have also tried pasting the htacess from public folder on root folder and removed the one from public folder but it dint work
actually the error which occurs is that my routes file skips all the patterns say if I have something like Route::get('/api/login', 'api#login') then it does works with urls containing public but when I remove public from url all the patterns are skipped. and it shows 404
I made sure all the requests are reaching public/index.php and mysterious thing is that when I print
var_dump(Request::root());
in routes.php it for url with public it says http://localhost/folderName/subFolderName/public but for the urls without public it says http://localhost/ and I think this is the reason why my patterns in routes file are not working.
P.S. I have set the title pretty urls laravel 5.4 instead of laravel routes not working because I think it is .htaccess which I needs to configure properly to get this working.
I do not want to change the folder structure because I believe in that there is some reason for which it is like this and There should be some work around for this.
Regarding setting vhosts I wont be able to do this on server (For I do not have the rights) so there is no use of setting a vhost on local as a work around as in login run I have to solve this.
Try this in your public/.htaccess
RewriteRule ^ /public/index.php [L]
And the .htaccess in root
RewriteRule ^(.*)$ /public/$1 [L]

Multiple Choices error page in 1and1

I have done my project using laravel. I want to upload my project on 1and1. I set my directory structure on 1and1 is as '/directory' which contains index.html file which links to the other page in /directory/EngageV1/public/auth/login. but when I visits this page I am getting the error page as,
Multiple Choices
The document name you requested (/index.php) could not be found on this server. However, we found documents with names similar to the one you requested.
Available documents:
/index.html (common basename)
Please consider informing the owner of the referring page about the broken link.
My .htaccess file looks like,
<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]
</IfModule>
If enter url as /directory/EngageV1/public/ then it shows me the welcome page which I have set in routes.php file for route as '/'. Only this page works fine,I don't know what to do, Please give any suggestion.
Replace default contents in your .htaccess file under your the public
folder with the following.
RewriteBase /
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
If you still have problems look at this tutorial about how to deploy a Laravel webapp to 1and1
I know this is is an very old topic, but it took us some time today to find out what's going wrong on an old 1and1 (now Ionos) server. Turned out, the "multiple choices" page doesn't com from Option +MultiViews / mod_negotiation, but from a module called mod_speling that's running there by default. You can disable it in the .htaccess file:
<IfModule mod_speling.c>
CheckSpelling off
</IfModule>

mod_rewrite not working correctly - accessing static resources

Right now I have a few folders (img, css, js, and ico) in the root directory of my website. However, I want to move them into a new directory called public_html. For example, instead of the image folder being located in /img, it would be instead located in /public_html/img. But I'm having problems doing this, and i suspect it's a problem with my .htaccess file.
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
ErrorDocument 404 static/404.html
ErrorDocument 403 static/403.html
RewriteCond %{REQUEST_FILENAME} !-f
# ---------- Custom Routes ----------------
# -----------------------------------------
RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2
RewriteRule ^(.*)$ index.php?r=$1 [L,QSA]
I can access /public_html/css/style.css just fine, but when I add in that first RewriteRule line and try to access /css/style.css, it doesn't work.
Can anyone figure out why? Thanks.
Add the [L] flag to the first rule. Otherwise it drops through to the second rule and attempts to pass it in r= to index.php. Accessing it via public_html works because of the RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(js|css|img|ico)\/(.+)$ public_html/$1/$2 [L]
# Update: Try using the RewriteCond before this line
# as well as where you have it earlier
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?r=$1 [L,QSA]

apache not serving static content correctly

I have been working on my own mvc framework to further my web app learning, but am having trouble serving static resources. I am trying to have a single entry point into the application, aka a front controller, so in my project / I have an .htaccess file that redirects all requests to the app/ folder where another .htaccess passes the request uri to index.php (in app/) who delegates the request to the appropriate controllers.
However, when I try to serve up static content, such as javascripts or cascading style sheets, I still get redirected through app/index.php. I am also getting "favicon.ico does not exist in /var/www" errors in /var/log/apache2/errors.log (maybe because of symlink to ~/www?). I do not expect to because of the following .htaccess file in the root directory of my project root:
<IfModule mod_rewrite.c>
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
# ----------------------------------------------------------------------
# Suppress the "www." at the beginning of URLs
# ----------------------------------------------------------------------
# The same content should never be available under two different URLs - especially not with and
# without "www." at the beginning, since this can cause SEO problems (duplicate content).
# That's why you should choose one of the alternatives and redirect the other one.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
#----------------------------------------------------------------------
# Route static resources to respective files
#----------------------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond public/$0 -f
RewriteRule ^.+\.(jpg|gif|png|ico|css|js)$ /public/$0 [L]
#----------------------------------------------------------------------
# Redirect all other requests to the app folder
#----------------------------------------------------------------------
RewriteRule ^$ app/ [L]
RewriteRule (.*) app/$1 [L]
</IfModule>
and here is the .htaccess in my app/ folder:
<IfModule mod_rewrite.c>
RewriteEngine On
# ensure request is not path to filename or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# redirect all requests to index.php?url=PATHNAME
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Why can't I serve static content correctly? This would make sense to me if I wasn't trying to sent all static requests to public/, which is where my css, jpg, png, js, etc files reside. But I have a RewriteCond rule in there to send the requests for such files to the public dir... Confusing?
Assuming, from what I understood, that your project structure is the following:
/
/.htaccess
/app/.htaccess
/app/index.php
/public/static.js (for example)
Here is what I come up with, hoping it'll solve your problem:
the .htaccess in the root folder:
<IfModule mod_rewrite.c>
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^public/.+\.(jpg|gif|png|ico|css|js)$ - [L]
RewriteRule ^(.*)$ app/$1 [L]
</IfModule>
And the .htaccess in the app folder is unchanged.
Every request starting with public and being a file with the listed extensions won't be redirected which is done with the dash character.
The last rule allows to redirect a request to the app/index.php file.
I think the resulting behaviour is the expected one:
static files in the public directory are not redirected,
files with another extension in the public directory will be
redirected to app/index.php (maybe for some error treatment),
requests not starting with public will be redirected to
app/index.php.

Categories