提问者:小点点

htaccess中的Wordpress和CI重写规则


我已经安装了WordPress在example.com.我还安装了CodeIgniter在ci文件夹这里example.com/ci/ci是文件夹名称,寄存器是控制器名称和CI的工作URL是example.com/ci/register。我的基本URL以https://开头。

现在我有一个WordPressURL示例。com/hotelhotel是我在WordPress admin中创建的页面,工作正常。

我想像example.com/hotel/ci/register一样运行我的CI路径,我想我们可以用一些重写规则来做,这样我的网址看起来就像example.com/hotel/ci/register。我已经添加了给定的htaccess为wordpress,重定向我在这里example.com/hotel/ci/register。它显示了CI的404错误。这意味着现在我在CI。现在我在routes.php文件中做了以下事情。

$route['default_controller'] = 'register';
$route['404_override'] = 'register';

下面是URL示例。com/hotel/ci/register正在工作,但这不是正确的方式,下次将有更多控制器,那么它将无法工作。

注意:我不能创建hotel文件夹,因为hotel是WordPress中的一个页面。如果我创建hotel文件夹,那么WordPress URLexample.com/hotel/将无法工作。它将重定向WordPress页面到hotel文件夹。所以我必须在不创建hotel文件夹的情况下做到这一点。注意example.com=myurl.com。

我需要找到另一个好的解决方案。任何建议或指导将不胜感激?

以下是我在wordpresshtaccess中的重写规则:

# BEGIN WordPress
   <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^/?hotel/ci/register(/.*)?$ /ci/$1 [L]
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
# END WordPress

下面是我的CIhtaccess

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 
</IfModule>

共3个答案

匿名用户

有趣的问题!我用Wordpress和Codeigniter的新安装设置了一个Docker容器,我在WP中创建了一个hotel页面,在CI中创建了一个注册控制器和视图,并进行了测试。我在这上面花了太多时间,但我确实找到了答案。

首先,你的Wordpress. htaccess.正如@tobiv在评论中指出的那样,你不应该在BEGIN/END WordPress评论之间添加任何东西,因为它可能会被WP更新破坏。您的重定向必须在标准WP规则之前,所以将其添加到文件的顶部:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^hotel/ci/register /ci/register [L]
</IfModule>

# BEGIN WordPress
# ... These are the default, unchnaged WP rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

您不需要修改Codeigniter。htaccess文件,默认文件就是您所需要的。它应该位于Codeigniter安装的根目录中,ci/。htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

此时https://example.com/hotel/ci/register将显示Codeigniter 404页面。如果您启用了日志记录(请参阅config/config.php),并查看日志文件,您将看到原因:

ERROR - 2017-11-14 17:57:20 --> 404 Page Not Found: Hotel/ci

这是整个问题的根源。由于初始重定向是内部重定向(意味着浏览器中显示的URL不会更改),因此要处理的URI Codeigniter接收到的是仍显示在浏览器地址栏中的URI-hotel/ci/register。Codeigniter将尝试通过两种方式处理这样的请求:

>

或者查找名为application/controllers/Hotel的控制器。php,使用名为ci的方法;

在我们的例子中,没有Hotel控制器,也没有路由来描述如何处理这样的请求,所以嘣,404。

一个简单的解决方案是创建一个路由来处理此请求:

$route['hotel/ci/register'] = 'register/index';

现在https://example.com/hotel/ci/register有效!

注意事项:

>

确保删除$route['404_override']='register'路线;

CIbase\u url与此问题无关,但显然应该设置。根据您希望链接的方式,我认为http://example.com/ci/http://example.com/hotel/ci/是正确的。

我不太清楚此条件在您的CI中的用途。htaccess用于:

RewriteCond $1 !^(index\.php|resources|robots\.txt)

现有的默认条件已经跳过磁盘上存在的文件和目录。这就是!-f!-d条件的意思-"如果请求的模式与磁盘上的文件或目录不匹配,则执行重定向"。

如果您在磁盘上有一个robots.txt文件(在您的ci/dir中),并且有人请求https://example.com/ci/robots.txt,则!-f条件将失败,并且重写是跳过-这意味着请求被处理没有重写和robots.txt返回成功。index.php也一样。如果您有一个名为ci/资源的目录,并且有人请求https://example.com/ci/resources,则!-d条件将失败,跳过重定向,并且请求成功。

我不确定您的资源部分,但也许您可以完全删除该条件。

如果您不需要漂亮的Codeigniter URL(我的意思是除了https://example.com/hotel/ci/register,此更改不会影响它),而且事实证明,您不需要上述附加条件,您可以删除CI。htaccess完全简化了事情。为此,只需将WordpressRewriteRule更改为非漂亮版本:

RewriteRule ^hotel/ci/register /ci/index.php/register [L]

并删除您的CI. hrecces

匿名用户

我认为你走在正确的道路上,试试这个:

# BEGIN WordPress
   <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^/hotel/ci(/.*)?$ /ci/$1 [L]  # remove the register part 
                                              # so that it would be handled by CI
                                              # also remove the "?" in front of hotel
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
# END WordPress

保持您的CI htaccess原样:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 
</IfModule>

最后一部分是匹配CIconfig。php到您的新url模型

$config['base_url'] = "http://example.com/hotel/ci/";

匿名用户

我不认为这将是你的问题的伟大答案,但如果我面临同样的问题,我会使用路由参见示例代码。

$route['(:any)/ci/register']  = "register";

上面的代码会做什么(: any)意味着第一个uri中的任何单词,之后你可以定义任何你想要的url i定义的ci/寄存器,你也可以这样做。

$route['(:any)/register']  = "here_you_can_add_your_controller/function";

这将工作,如果点击像这样的url。

http://www.example.com/any_word_you_want/register

它将击中你的控制器功能.你需要回声的东西,它将显示在你的浏览器。

你也可以在你的基本url中定义hotel这个词,如@am05mhz在他的回答中所示,但我认为这不是一个好主意,因为将来你的url中可能还会有两个词。

注意:以上代码示例仅在中有效。htaccess按照问题中的提供路由访问权限。htaccess为您服务。有关路由的完整知识,请查看codeigniter URI路由的文档