【问题标题】:Why isn't this mod_rewrite re-directing?为什么这个 mod_rewrite 不重定向?
【发布时间】:2016-05-01 02:36:51
【问题描述】:

我通过 index.php 发送每个请求,但我的博客子目录中的页面除外。我已经能够在我的父文件夹中使用 mod_rewrite 来做到这一点,并且;

    RewriteCond %{REQUEST_URI} !^/blog
    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

]

但是,如果请求具有以下形式,我也想将请求发送到我的博客文件夹:

文档/一些文件。

我试过了:

RewriteRule ^/documentation/(.+)$ https://www.some_domain.com/blog/documentation/$1

但它看起来在这种情况下我的请求没有发送到博客文件夹。我的完整代码如下:

RewriteEngine On

#redirect to index.php as appropriate
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^/documentation/(.+)$ https://www.some_domain.com/blog/documentation/$1

RewriteCond %{REQUEST_URI} !^/blog

RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

编辑:

我在下面使用了@Rijul 建议的稍微修改的版本,在将 RewriteRule 移到 RewriteCond 之前,它可以按我希望的那样工作。换句话说,对文档的重写执行对博客子文件夹的重写。而且,所有其他请求都通过我的 index.php 文件。在这一点上,我想了解原因。

RewriteEngine On
RewriteRule ^documentation/?(.*)$ /blog/documentation/$1 [R=301,L]

#redirect to index.php as appropriate
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l


RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

【问题讨论】:

    标签: mod-rewrite


    【解决方案1】:

    据我所知

    RewriteEngine On
    

    如果没有这个,所有的重写规则和条件都将被忽略

    RewriteRule ^documentation/?(.*)$ /blog/documentation/$1 [R=301,L]
    

    将文档重写为博客/文档

    RewriteCond %{REQUEST_FILENAME} !-d
    

    此重写条件检查请求的目录名称是否不存在。如果不退出。然后进入下一个条件

    RewriteCond %{REQUEST_FILENAME} !-f
    

    此重写条件检查请求的文件名是否不存在。如果不退出。然后进入下一个条件

    RewriteCond %{REQUEST_FILENAME} !-l
    

    此重写条件检查请求的符号链接是否不存在。如果不退出。然后继续重写规则

    RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
    

    重写为 index.php?url=

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    

    这样写重写条件会产生一个与操作。

    (没有以该名称存在的文件)和(没有以该名称存在的目录)和 (该名称中没有符号链接)

    如果是这样,则重写为 php 文件。 (没有以该名称存在的目录)/blog 的情况下将变为 false(因为这样的目录存在)

    【讨论】:

    • 成功了一半!现在,如果我放入文档/东西,它会很好用。但是,现在它没有执行我的任何“非文档”重定向。例如,我不相信它会在“一般”情况下进入最后一行。
    • 您能否提供一个不起作用的示例网址及其预期结果
    • 例如,www.my_domain.com/authenticate/show-user-login-form 应该转到我的登录页面(它没有上面的代码)。或者,像 www.my_domain.com 这样基本的东西应该通过 index.php 路由并显示主页。两者都没有发生。
    • 所以,我一直在玩弄它,并将文档重写行移到 #redirect 到 index.php 之前的右侧,它似乎正在工作。如果您或其他人可以解释为什么它现在可以工作,那么我很乐意将其作为解决方案。
    • 在进行更改后,您的 www.my_domain.com/blog 是否仍按预期工作?
    猜你喜欢
    • 2011-10-03
    • 1970-01-01
    • 2016-02-02
    • 2013-11-14
    • 2011-08-21
    • 2017-06-12
    • 2018-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多