【问题标题】:HTACCESS rules redirection errorHTACCESS 规则重定向错误
【发布时间】:2016-07-01 08:05:49
【问题描述】:

在我的 htaccess 文件中,我添加了一条规则,将 url/abc/xyz 重定向到 url/abc/xyz.php,所以现在即使我尝试访问 url/abc.php,它也会将我重定向到 url/abc/

帮我解决这个问题。我现在的代码:

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]

## To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]

我想要的是:

url/abc.php & url/abc 应该转到 url/abc.php

url/abc.php/#x & url/abc/#x 应该转到 url/abc.php/#x

【问题讨论】:

  • 403 禁止错误,因为它试图访问不存在的位置。
  • 全部位于根目录。 domain.com/abc 导致domain.com/abc.php 正确。问题在于 domain.com/abc.php/#x 被重定向到 domain.com/abc/#x
  • 为什么要让 url/abc.php 指向 url/abc.php?为什么不直接消除在地址栏中使用 .php 的可能性?
  • 是的,我将完全消除 .php,但我拥有它以便 url/abc/#x(或 url/abc.php/#x)被重定向到 url/abc.php/# x
  • 您确定收到的是 403 错误而不是 404 错误吗?

标签: php .htaccess redirect


【解决方案1】:

如果我正确解释了您的意图,以下重写规则应该符合您的意愿:

# Enable rewriting
RewriteEngine On

# Define site root
RewriteBase /

# Do not rewrite for image, css or js files
RewriteRule \.(css|jpe?g|gif|png|js)$ - [L]

# Add index.php to requests ending with a trailing slash
RewriteRule ^([a-z0-9_\-/]*/)?$ $1index.php [NC,R=301,L]

# Add .php to the end of a request without a trailing slash
RewriteRule ^(([a-z0-9_\-]+/)*[a-z0-9\-]+)$ $1.php [NC,R=301,L]

规则的作用:

  • 相对于域的根 (/) 进行重写
  • 重写规则将忽略具有规则 nr 2 中列出的文件扩展名的任何文件。
  • http://example.com/(或http://example.com)、http://example.com/foo/http://example.com/foo/bar/的请求将分别重写为http://example.com/index.phphttp://example.com/foo/index.phphttp://example.com/foo/bar/index.php
  • http://example.com/bazhttp://example.com/baz/boo 的请求将分别重写为http://example.com/baz.phphttp://example.com/baz/boo.php

【讨论】:

    【解决方案2】:

    URL 片段 (#) 永远不会发送到服务器,因此您不能使用 mod_rewrite 来重定向它。换句话说,# 仅在浏览器中被解释,因此您不必担心 .htaccess 中的它。

    此代码应该适合您,或者至少可以帮助您入门:

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME}\.php -f
        RewriteRule ^(.*)$ $1.php
    </IfModule>
    

    我还将它包含在 IfModule 中,以检查是否首先启用了 mod_rewrite。

    然而,我首选的方式是将所有内容重定向到 index.php,然后在其中放置 PHP 代码,该代码将分解 URL 并重定向到适当的页面。这非常适用于模型-视图-控制器 (MVC) 系统和其他类似的变体。这也允许从一个地方而不是多个地方加载通用站点设置。以下是我将用于此类设置的代码:

    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteCond %{REQUEST_URI} !^/img/[0-9a-zA-Z_\-]+\.(gif|jpg|png)$
      RewriteCond %{REQUEST_URI} !^/css/[0-9a-zA-Z_\-]+\.css$
      RewriteCond %{REQUEST_URI} !^/js/[0-9a-zA-Z_\-]+\.js$
      RewriteRule ^.+$ index.php [L]
    </IfModule>
    

    前三行表示不重定向指定目录中的任何图像、css 或 js 文件。

    【讨论】:

    • 好的,还有其他方法可以实现吗?我对 htaccess 很陌生。
    • @AniMenon 我已经进一步编辑了我的示例。第一个应该适合你,但第二个是我的首选方法。
    • 你的第二个例子不安全,会导致重写循环错误,因为 /index.php 也匹配重写模式。
    • @starkeen 我目前正在我的一些网站上实现此功能,它似乎运行良好。
    • @kojow7 谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-22
    • 2011-01-05
    • 1970-01-01
    相关资源
    最近更新 更多