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?
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/.htaccess
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 contentsAdd
app/helpers.php
to the autoload process by editingcomposer.json
If you have command line access, update PHP composer
Here is an approach that I took when encountered a similar situation. Install your
wordpress
inpublic_html
directory and preferably place yourlaravel application
with the WP directory:Best approach is to follow the Laravel Service Provider Architecture. Write a service provider for including the
wp-load.php
and use theboot function
to load assets:And add the service provider to your laravel app config,
config/app.php:
Now we have access to our WordPress functions within Laravel and can alter laravel
views
to something like:Your wordpress site should be accessible at
www.urdomain.com
and laravel app under urlwww.urdomain.com/app/appointment
or adjust you.htaccess
if you wish a different URL pattern. Hope this help.You can create symlink to Wordpress public directory in Laravel folder. For example,
wp
:And describe Laravel routes in .htaccess. Example of code of
/var/www/laravel/public/.htaccess
:Code of
/var/www/wordpress/public_html/.htaccess
(just copy of your wordpress .htaccess):