Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow WordPress and Laravel to share top level URLs with htaccess

Edit: This is a major edit of my question, based on feedback in the comments, and a bit of reading on meta.stackoverflow.

I've shifted my goals, now my priority is to route top level URLs between Laravel and WP: I no longer seek to share code / databases / user creds, etc.

Question: What rewrite rules do I need to achieve a URL structure like this:

/            // (home) delivered by WP
/content1    // WP
/content(n)  // WP
/dashboard   // delivered by Laravel
/widgets/mywidget/    // Laravel
  • all Laravel pages will be in /admin or /widgets, everything else will be handled by WordPress, including 404s
  • I will keep WP and Laravel completely separate.
  • My colleague and I will edit WP using Admin accounts, there will be no other WP users
  • I will manage presentation for the two apps completely separately

I've setup my files as follows, and have tested that each application works independently of one another. (Ie if Laravel's rewrite rules are the only ones in use, Laravel works fine using laravel-index.php)

From the web root: public_html/

/wordpress
/laravel3   // app dir
/css        // these are from the laravel public dir 
/images     //
/js, etc    //
/index.php  // WP index.php
/laravel-index.php    // laravel's index.php

I'm in a shared hosting environment with no access to httpd.conf.

Here is my .htaccess

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^widgets  -  [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

# Laravel
<IfModule mod_rewrite.c>
RewriteRule !^widgets - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ laravel-index.php/$1 [L]
</IfModule>

In this way:

  • I can reach my WordPress pages, and
  • WP serves a WP-404 for unknown URLs, but
  • /widgets gets a vanilla server 404, whilst
  • /laravel-index.php/widgets displays the proper content.

An additional problem is that WP Dashboard is stealing requests to /dashboard, which I'd like to redirect to Laravel

Thanks for reading!

like image 866
ptim Avatar asked Jan 27 '13 03:01

ptim


1 Answers

For WordPress, you need to send all requests that are not in the widgets or dashboard folders to index.php

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond !^widgets
RewriteCond !^dashboard
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

For Laravel, you need to do the opposite, send ONLY requests that are inside widgets or dashboard to laravel-index.php

# Laravel
<IfModule mod_rewrite.c>
RewriteCond ^widgets
RewriteCond ^dashboard
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ laravel-index.php/$1 [L]
</IfModule>
like image 135
bradym Avatar answered Nov 08 '22 06:11

bradym