【问题标题】:How to allow WordPress and Laravel to share top level URLs with htaccess如何允许 WordPress 和 Laravel 与 htaccess 共享顶级 URL
【发布时间】:2013-01-27 03:47:43
【问题描述】:

编辑: 这是我的问题的主要编辑,基于 cmets 中的反馈,以及对 meta.stackoverflow 的一些阅读。

我已经改变了我的目标,现在我的首要任务是在 Laravel 和 WP 之间路由顶级 URL:我不再寻求共享代码/数据库/用户凭据等。

问题:我需要什么重写规则来实现这样的 URL 结构:

/            // (home) delivered by WP
/content1    // WP
/content(n)  // WP
/dashboard   // delivered by Laravel
/widgets/mywidget/    // Laravel
  • 所有 Laravel 页面都在 /admin 或 /widgets 中,其他一切都将由 WordPress 处理,包括 404s
  • 我会让 WP 和 Laravel 完全分开。
  • 我和我的同事将使用管理员帐户编辑 WP,不会有其他 WP 用户
  • 我将完全分开管理这两个应用程序的演示

我已按如下方式设置了我的文件,并测试了每个应用程序是否可以相互独立地工作。 (即如果 Laravel 的重写规则是唯一使用的规则,Laravel 使用 laravel-index.php 可以正常工作)

来自网络根目录: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

我在一个无法访问 httpd.conf 的共享主机环境中。

这是我的 .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>

这样:

  • 我可以访问我的 WordPress 页面,并且
  • WP 为未知 URL 提供 WP-404,但是
  • /widgets 得到一个普通服务器 404,而
  • /laravel-index.php/widgets 显示正确的内容。

另一个问题是 WP Dashboard 正在窃取对 /dashboard 的请求,我想将其重定向到 Laravel

感谢阅读!

【问题讨论】:

    标签: url-rewriting .htaccess


    【解决方案1】:

    对于 WordPress,您需要将所有不在小部件或仪表板文件夹中的请求发送到 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
    

    对于 Laravel,您需要做相反的事情,只将小部件或仪表板内部的请求发送到 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>
    

    【讨论】:

    • @memeLab 我很好奇你在放弃 WP 时在 Laravel 中使用了什么博客类型选项。以及您从现有 WP 数据中获得的任何转化。
    • 嗨@MECU,我选择了一个完全自定义的系统,所以我没有使用任何可用的作曲家捆绑包,并且没有现有数据,抱歉:)
    猜你喜欢
    • 2016-06-09
    • 2016-12-05
    • 2021-06-11
    • 2011-02-12
    • 1970-01-01
    • 2018-04-19
    • 2023-03-31
    • 1970-01-01
    • 2012-06-24
    相关资源
    最近更新 更多