【问题标题】:mod_rewrite: Being redirected to root folder; I want it to stay in sub-foldermod_rewrite:被重定向到根文件夹;我希望它留在子文件夹中
【发布时间】:2011-09-18 05:30:18
【问题描述】:

我在这个目录 /var/www/example.com/ 中有一个 Zend Framework 站点,并将 WP 博客放在这个目录 /var/www/example.com/public/blog/

以下是重要文件...

/var/www/example.com/public/.htaccess 包含:

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^blog - [NC,L] #Just added this line, it was previous not there
RewriteRule ^.*$ index.php [NC,L]

那么我在/blog/文件夹中的.htaccess是这样的:

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

在 zend 站点周围单击可以正常工作。进入 /blog/ 子文件夹可以很好地加载 WP 博客。但是当我点击博客中的任何帖子时,它会被重定向到 ZF 应用程序,并且不会停留在 wordpress 中。

我追求的最终结果是将两者分开,但要确保它们都重写 index.php,使其不会在 URL 中呈现。

我对 htaccess 没有很好的经验,所以我不确定发生了什么。这是我的 VHost,只是为了衡量。

<VirtualHost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/var/www/example.com/public"
ServerName example.com
ErrorLog "logs/example.local-error.log"
CustomLog "logs/example.local-access.log" common

<Directory "/var/www/example.com/public" >
         Options Indexes FollowSymLinks
         AllowOverride All
         Order allow,deny
         allow from all

</Directory>

<Location />
        RewriteEngine on
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule !\.(js|ico|gif|jpg|png|css)$ /index.php
</Location>

SetEnv APPLICATION_ENV development

</VirtualHost>

注意由于 VHost 中的重写规则,我删除了 /public/ 文件夹中的 .htaccess 进行测试,但我的问题仍然存在。

请求和响应 URL 与浏览器中显示的相同:

http://example.local/blog/my-test-blog-post/

但是,浏览器呈现来自 ZF 的错误,表明它被定向到 public/index.php 文件,而不是预期的 public/blog/index.php 文件。我还使用 Netbeans 和 XDebug 来单步调试代码。我在两个 index.php 文件的第一行都设置了断点,当点击博文链接时,它会立即加载 ZF index.php,而不是 /blog/index.php

如果我使用http://example.local/blog/?p=695 之类的 URL,它会出现,它可以工作并且不会路由到 ZF 应用程序。但是当我在 wp-admin 中使用 http://example.local/blog/my-test-blog-post/ 之类的自定义永久链接时,它会尝试路由到 ZF。

环顾谷歌,它告诉我/blog/ 文件夹中的 htaccess 不正确。 Options FollowSymLinks & AllowOverride All 均已设置。我只是无法正确... http://wordpress.org/support/topic/custom-permalinks-not-working-1

我还将添加 mod_rewrite IS 工作,因为我可以很好地点击 ZF 应用程序。

最后一点,此永久链接适用于 WP:

/index.php/%post_id%/%postname%/

但我想在没有索引文件的情况下使用它: /%post_id%/%postname%/

mod_rewrite 日志:

1(2) init rewrite engine with requested uri /blog/698/test-blog-post/
(1) pass through /blog/698/test-blog-post/
(3) [perdir /] add path info postfix: D:/www/example/public/blog/698 -> D:/www/example/public/blog/698/test-blog-post/
(3) [perdir /] applying pattern '\.(js|ico|gif|jpg|png|css)$' to uri 'D:/www/example/public/blog/698/test-blog-post/'
(4) [perdir /] RewriteCond: input='D:/www/example/public/blog/698' pattern='!-f' => matched
(4) [perdir /] RewriteCond: input='D:/www/example/public/blog/698' pattern='!-d' => matched
(2) [perdir /] rewrite 'D:/www/example/public/blog/698/test-blog-post/' -> '/index.php'
(2) [perdir /] trying to replace prefix / with /
(5) strip matching prefix: /index.php -> index.php
(4) add subst prefix: index.php -> /index.php
(1) [perdir /] internal redirect with /index.php [INTERNAL REDIRECT]
(2) init rewrite engine with requested uri /index.php
(1) pass through /index.php
(3) [perdir /] applying pattern '\.(js|ico|gif|jpg|png|css)$' to uri 'D:/www/example/public/index.php'
(4) [perdir /] RewriteCond: input='D:/www/example/public/index.php' pattern='!-f' => not-matched
(1) [perdir /] pass through D:/www/example/public/index.php

另一个 mod_rewrite 日志在尝试了 Jason Dean 的建议后(没用)

(2) init rewrite engine with requested uri /blog/698/test-blog-post/
(1) pass through /blog/698/test-blog-post/
(3) [perdir /] add path info postfix: D:/www/example/public/blog/698 ->  D:/www/example/public/blog/698/test-blog-post/
(3) [perdir /] applying pattern '\.(js|ico|gif|jpg|png|css)$' to uri 'D:/www/example/public/blog/698/test-blog-post/'
(4) [perdir /] RewriteCond: input='D:/www/example/public/blog/698' pattern='!-f' => matched
(4) [perdir /] RewriteCond: input='D:/www/example/public/blog/698' pattern='!-d' => matched
(2) [perdir /] rewrite 'D:/www/example/public/blog/698/test-blog-post/' -> '/index.php'
(2) [perdir /] trying to replace prefix / with /
(5) strip matching prefix: /index.php -> index.php
(4) add subst prefix: index.php -> /index.php
(1) [perdir /] internal redirect with /index.php [INTERNAL REDIRECT]
(2) init rewrite engine with requested uri /index.php
(1) pass through /index.php
(3) [perdir /] applying pattern '\.(js|ico|gif|jpg|png|css)$' to uri 'D:/www/example/public/index.php'
(4) [perdir /] RewriteCond: input='D:/www/example/public/index.php' pattern='!-f' => not-matched
(1) [perdir /] pass through D:/www/example/public/index.php

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    尝试添加:

    RewriteCond %{REQUEST_URI} !^/blog
    

    到你的第一个 .htaccess

    我认为它正在拦截对您博客子文件夹的任何请求。

    【讨论】:

    • 感谢您的意见。我试过了,但是没有用。将重写日志添加到我原始帖子的底部。我可能会补充一点,我认为根目录 htaccess 不会拦截它。当我去example.local/blog 它呈现wordpress 就好了。当我点击任何帖子链接时。如果我使用 WP 默认永久链接 (example.local/blog/?p=138),链接有效,但如果我尝试 example.local/blog/138/test-blog-post,则链接无效。看起来好像 VHost 中的重写语句正在捕获它。但是,如果我在 VHost 中删除该部分,整个网站就会中断。
    • 哈,说出来吧,一旦我删除了 vhost 中的规则,它似乎就可以工作了!
    猜你喜欢
    • 2017-03-09
    • 2015-10-06
    • 2016-06-27
    • 2011-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-23
    • 2018-05-28
    相关资源
    最近更新 更多