I have a Laravel project in my public_html folder. The domain is for example domain.com My .htaccess file (in public_html folder) is like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
There are the following routes:
api/licenseplate
api/calendar
auth/login
admin/settings
admin/appointments
appointment
...
So an example of a URL is http://domain.com/appointment.
Now I would like to have a wordpress website on domain.com. So when you go to domain.com you see the wordpress website. But I also want to have the urls like /appointment of my laravel project.
What's the easiest and cleanest way to do this?
You can create symlink to Wordpress public directory in Laravel folder. For example, wp:
/var
/www
/laravel
/public
/wp #(symlink to -> /var/www/wordpress/public_html)
index.php
.htaccess
/wordpress
/public_html
index.php
.htaccess
And describe Laravel routes in .htaccess. Example of code of /var/www/laravel/public/.htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{ENV:REDIRECT_FINISH} .
RewriteRule ^ - [L]
# Laravel
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} !.*\.php$
RewriteRule ^(.*)$ $1 [E=FINISH:1,L]
RewriteCond %{REQUEST_URI} ^/(api/licenseplate)(\?.*|$) [OR]
RewriteCond %{REQUEST_URI} ^/(api/calendar)(\?.*|$) [OR]
RewriteCond %{REQUEST_URI} ^/(admin/settings)(\?.*|$) [OR]
RewriteCond %{REQUEST_URI} ^/(admin/appointments)(\?.*|$) [OR]
RewriteCond %{REQUEST_URI} ^/(appointment)(\?.*|$) [OR]
RewriteCond %{REQUEST_URI} ^/(auth/login)(\?.*|$)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [E=FINISH:1,L]
# Wordpress
RewriteCond %{REQUEST_URI} !^/wp
RewriteRule ^(.*)$ /wp/$1 [E=FINISH:1,L]
</IfModule>
Code of /var/www/wordpress/public_html/.htaccess (just copy of your wordpress .htaccess):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Server Structure
.htaccess files provide a way to make configuration changes on a per-directory basis. This means that you only need to direct the request to the directory that will have rules for how to best process the request. Thus you will leave the .htaccess files that came with both Laravel and WordPress in their directories. Just create another one for the parent directory that holds them both.
The below assumes that the full path of public_html is /var/www/public_html.
Folder Structure
/var/www/public_html/laravel
/var/www/public_html/wp
/var/www/public_html/.htaccess
# Limit Access To Laravel Public Folder
<Directory "/var/www/public_html/laravel">
Order Deny,allow
Deny from all
</Directory>
<Directory "/var/www/public_html/laravel/public">
Allow from all
</Directory>
# Rewrite Requests
<IfModule mod_rewrite.c>
RewriteEngine On
# Do Not Rewrite Directory And File Requests
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ $1 [L]
# Rewrite Laravel Routes
RewriteCond %{REQUEST_URI} api/licenseplate [OR]
RewriteCond %{REQUEST_URI} api/calendar [OR]
RewriteCond %{REQUEST_URI} admin/settings [OR]
RewriteCond %{REQUEST_URI} admin/appointments [OR]
RewriteCond %{REQUEST_URI} appointment [OR]
RewriteCond %{REQUEST_URI} auth/login
RewriteRule ^(.*)$ laravel/public/$1 [L]
# Rewrite To WordPress For Everything Else
RewriteRule ^(.*)$ wp/$1 [L]
</IfModule>
Application Settings
Both Laravel and WordPress have ways to generate links to their assets (images, CSS, JavaScript).
WordPress is built to easily allow for installation to a sub-directory so you only need to change the WordPress Address and Site Address on the General Settings Screen.
Laravel assumes that it is installed to the top directory. The asset() helper is used to generate paths to asset files. So you will need to override the default function with one of your own. Here is how:
Create app/helpers.php with these contents
/**
* Generate an asset path for the application
* with the installation sub-directory
* prepended to the path.
*
* #param string $path
* #param bool $secure
* #return string
*/
function asset($path, $secure = null)
{
$path = ltrim($path, '/');
$dir = basename(dirname(__DIR__)) . '/public';
$url = app('url')->asset($path, $secure);
return str_replace($path, $dir . '/' . $path, $url);
}
Add app/helpers.php to the autoload process by editing composer.json
"autoload": {
//...
"files": [
"app/helpers.php"
]
},
If you have command line access, update PHP composer
composer dump-autoload
Here is an approach that I took when encountered a similar situation. Install your wordpress in public_html directory and preferably place your laravel application with the WP directory:
wp-admin/
wp-content/
wp-includes/
... other wordpress files ...
app/ <-- laravel application
|-> app/
|-> bootstrap/
|-> config/
|-> public/
|-> ... other laravel files and directories ...
Now to get the WordPress functions available within your Laravel
application, include wp-load.php within your Laravel installation
and then you can use the enqueueing functions from WordPress to
load the assets you need.
Best approach is to follow the Laravel Service Provider Architecture. Write a service provider for including the wp-load.php and use the boot function to load assets:
// app/Providers/WordPressServiceProvider.php
class WordPressServiceProvider extends ServiceProvider {
protected $bootstrapFilePath = '../../wp-load.php'; // Adjust ur path
public function boot() {
// Load assets
wp_enqueue_style('app', '/app/public/app.css'); // Adjust ur path
}
public function register() {
// Load wordpress bootstrap file
if(File::exists($this->bootstrapFilePath)) {
require_once $this->bootstrapFilePath;
} else throw new \RuntimeException('WordPress Bootstrap file not found!');
}
}
And add the service provider to your laravel app config, config/app.php:
/* ... */
'providers' => [
// ...
/*
* Application Service Providers...
*/
// ...
App\Providers\WordPressServiceProvider::class,
Now we have access to our WordPress functions within Laravel and can alter laravel views to something like:
{!! get_header() !!}
<div class="container">
<div class="content">
<div class="title">Laravel 5</div>
<div class="quote">{{ Inspiring::quote() }}</div>
</div>
</div>
{!! get_footer() !!}
Your wordpress site should be accessible at www.urdomain.com and laravel app under url www.urdomain.com/app/appointment or adjust you .htaccess if you wish a different URL pattern. Hope this help.
Related
I've wordpress project in (html_public) folder, and my domain run project fine, I want integrate laravel8 project inside the wordpress, so that both run in the same domain like That:
WordPress URLs:
https://example.com/wordpressHomepage
https://example.com/AnotherwordpressPage
Laravel URLs:
https://example.com/public/LaravelPage
https://example.com/public/LaravelPage/Id
Wordpress && LaravelApp path in cpanel:
public_html/
wp-admin
wp-content/
wp-includes/
... other wordpress files ...
laravelApp/ <-- laravel application folder
|-> app/
|-> bootstrap/
|-> config/
|-> public/
|-> ... other laravel files and directories ...
Wordpress .haccess:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Laravel .haccess in /public:
<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]
</IfModule>
wordpress index.php:
<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* #package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* #var bool
*/
define( 'WP_USE_THEMES', true );
/** Loads the WordPress Environment and Template */
require __DIR__ . '/wp-blog-header.php';
Laravel index.php in App/public/index.php:
<?php
use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Check If The Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is in maintenance / demo mode via the "down" command
| we will load this file so that any pre-rendered content can be shown
| instead of starting the framework, which could cause an exception.
|
*/
if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
require $maintenance;
}
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/
require __DIR__.'/../vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request using
| the application's HTTP kernel. Then, we will send the response back
| to this client's browser, allowing them to enjoy our application.
|
*/
$app = require_once __DIR__.'/../bootstrap/app.php';
$kernel = $app->make(Kernel::class);
$response = $kernel->handle(
$request = Request::capture()
)->send();
$kernel->terminate($request, $response);
please any help?
Create one folder in the root directory and dump your laravel project folder in that and create .htaccess inside laravel project folder then point that .htaccess to the laravel public directory index file. your project will run smoothly.
But my suggestion is to go with VPS hosting which will have you root access.
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
Assuming your Laravel .htaccess file is inside the /public subdirectory then simply remove (or comment out) the first two rules.
For example:
#RewriteCond %{REQUEST_FILENAME} -d [OR]
#RewriteCond %{REQUEST_FILENAME} -f
#RewriteRule ^ ^$1 [N]
#RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
#RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
This does assume that server.php is also located inside the /public subdirectory.
I am trying to deploy my Laravel 8 app to a folder in CPANEL (first time I've deployed Laravel). I have searched the forum and made changes as the many posts direct. I am getting 404 errors across all pages apart from the homepage and login and register pages. This is a common error but I cant find a solution. Dont shoot me.
1.All files uploaded to url/laravel/blog
Public folder moved outside and renamed public_html
The /laravel folder now has two directories /blog and /public_html
index.php in the public_html folder updated:
require DIR.'/../blog/vendor/autoload.php';
$app = require_once DIR.'/../blog/bootstrap/app.php';
New .htaccess file added to the root of the public_html and set to chmod 755 (detailed below)
Renamed the cache folder in the /bootstrap folder so that new cache is created (I hope)
Database is linked within the .env file
I am out of ideas, I have created a test html file in the public folder and I can see this in the browser when I navigate to it. I am learning and any help will be appreciated.
<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 needed to point the subdomain/add-on domain to the public folder. No need to move anything or change Htaccess etc.... Credit to https://stackoverflow.com/users/2325620/james-clark-developer
Try this in your .htaccess
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^$ public/index.php [L]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
</IfModule>
I have a Laravel Apps and a shared hosting. I wanted to deploy my Laravel apps to cpanel, and got an error
404 resources not found when deployed. I have put the laravel public folder into the root folder, and the laravel file into subfolder /laravelApp.
So my hosting structured like this :
bdd.services
|--> public_html
|----> myLaravelApp
|------> css
|------> js
|------> laravelApp
|--------> app
|--------> /* and all the laravel apps inside here */
|------> .htaccess
|------> index.php
|
|
|----> someonesProject
When I tried to test the app, I see the request was requesting to
https://mydoamin.com/login/auth where it should be https://mydoamin.com/myLaravelApp/login/auth. And it shows an error 404 resources not found.
So how to make the routing always go to https://mydoamin.com/myLaravelApp/? or maybe there's another solution?
here's my .htaccess
<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_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I've also set the APP_URL at the .env.
APP_URL=https://mydoamin.com/myLaravelApp/
In this case you should ensure that you have
APP_URL="http://example.com/folder"
In your .env file
Then instead of doing an href like this
href="/login"
Give your route a name
Route::get('login', ...)->name('login');
and do your href like this
href="{{ route('login') }}"
This way it will always use the full url
I have recently installed Laravel into my public_html folder. I want it so when I log into www.malhub.com it gets the contents of the public folder (public_html/public).
After trial and error, I was able to get it working somewhat. Now I have a 404 error, which is caused because the site (www.malhub.com) resolves to :
http://malhub.com/public/public_html
But my .htaccess code states:
RewriteRule ^(.*)$ public_html/public/$1 [L]
In other words, it looks for a public_html folder within the public folder (should just be the public folder)
I added some folders (test) just to make sense of how this works and am befuddled. It is going into the public folder and looking for another folder.
When I just try writing:
RewriteRule ^(.*)$ public/$1 [L]
or "public_html" or any combination of /../../ I keep getting 500 errors (that don't show the url).
How does this line of code work, and what is the optimum way?
By default the website will be load from public folder.
If you want to remove public from your url,copy .htaccess file from public folder to root and replace the code with the following..
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php
If you don't want to remove all files and folder from the public folder, then just copy .htaccess to your root directory and rename server.php to index.php and last one step is if all resource files in /public directory couldn't find and request URLs didn't work for using asset() helper. Then, you need to add public to your helpers.php file.
function asset($path, $secure = null)
{
return app('url')->asset($path, $secure);
}
To
function asset($path, $secure = null)
{
return app('url')->asset("public/".$path, $secure);
}
You will fine the helpers.php in vendor/laravel/framework/src/Illuminate/Foundation/helpers.php.
I have in my site a Wordpress running in the root directory, ie: mysite.com/
And I need to create a ZF Application, and host it under a subdirectory of my hosting, ie: mysite.com/backend
I did under my root directory:
% zf create project backend
It created the "backend" directory under my root, with all the project inside.
Under my root directory I have this .htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Under webroot/backend/public I have this .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I'm using ZF 1.11
What I can't figure how must I configure is the .htaccess files to get this zf app working under the subdirectory.
Regards!
Currently you have a folder backend . So it looks
mysite.com/backend
Now when you create project with
zf create project <project name>
you will be getting a directory structure . From that move the index.php and .htaccess ( from the public folder ) to the backend folder.
Upload all the remaining folder to the one which is not accessible via web . ie below the web root directory ( public or what ever you call )
Change the APPLICATION_PATH in the index.php of the backend folder according to the where your project folder is now.
Its very easy with zend framework. Hope this will help you.
May be you want to look whether wordpress .htaccess will allow the directory which is already there .
I had to put this .htacess inside mysite.com/backend
RewriteEngine On
RewriteRule !\.(csv|js|gif|jpg|png|css|txt)$ public/index.php [L]
And then in my Bootstrap.php this:
public function run() {
$front = $this->getResource('FrontController');
$front->setBaseUrl('/backend');
}
So Zend can route the requests in the right way.