【问题标题】:Remove php extension of website with htaccess使用 htaccess 删除网站的 php 扩展名
【发布时间】:2016-01-19 11:30:11
【问题描述】:

我的网站上有这些链接:

http://crw4u.be/werkgever.php (我只能在我的问题中添加 2 个网址,因为我是 n00b)

我在 htaccess 中添加了一个代码,这样我就可以在没有 php 的情况下访问这些页面:

http://crw4u.be/werkgever

这段代码可以正常工作:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

但我在我的 php 文件中使用了某些形式:

<form name="planning" enctype="multipart/form-data" method="post" action="email/index.php">

我在发送带有在 htaccess 中添加的代码的表单时遇到错误:

未找到。 在此服务器上找不到请求的 URL /email/。

有谁知道如何修改这段代码?我一直在搜索和测试,但似乎无法解决这个问题。

非常感谢!

托马斯

【问题讨论】:

    标签: php apache .htaccess


    【解决方案1】:

    也添加此代码,它将让您访问目录。

    RewriteCond %{REQUEST_FILENAME} !-d
    

    【讨论】:

      【解决方案2】:

      试试这个规则:-

      RewriteEngine on 
      RewriteCond %{REQUEST_FILENAME} !-d 
      RewriteCond %{REQUEST_FILENAME}\.php -f 
      RewriteRule ^(.*)$ $1.php
      

      以下是规则如何工作的基本描述:-

      正则表达式

      . (any character)
      * (zero of more of the preceding)
      + (one or more of the preceding)
      {} (minimum to maximum quantifier)
      ? (ungreedy modifier)
      ! (at start of string means "negative pattern")
      ^ (start of string, or "negative" if at the start of a range)
      $ (end of string)
      [] (match any of contents)
      - (range if used between square brackets)
      () (group, backreferenced group)
      | (alternative, or)
      \ (the escape character itself)
      

      使用正则表达式,可以在 URL 中搜索各种模式并在匹配时重写它们

      标志

      标记被添加到重写规则的末尾,以告诉 Apache 如何解释和处理规则。它们可用于告诉 apache 将规则视为不区分大小写,如果当前规则匹配则停止处理规则,或各种其他选项。它们以逗号分隔,并包含在方括号中。这是标志的列表,以及它们的含义。

      C (chained with next rule)
      CO=cookie (set specified cookie)
      E=var:value (set environment variable var to value)
      F (forbidden - sends a 403 header to the user)
      G (gone - no longer exists)
      H=handler (set handler)
      L (last - stop processing rules)
      N (next - continue processing rules)
      NC (case insensitive)
      NE (do not escape special URL characters in output)
      NS (ignore this rule if the request is a subrequest)
      P (proxy - i.e., apache should grab the remote content specified in the substitution section and return it)
      PT (pass through - use when processing URLs with additional handlers, e.g., mod_alias)
      R (temporary redirect to new URL)
      R=301 (permanent redirect to new URL)
      QSA (append query string from request to substituted URL)
      S=x (skip next x rules)
      T=mime-type (force specified mime type)
      

      例外和特殊情况

      重写条件可以通过几种不同的方式进行测试——它们不需要被视为正则表达式模式,尽管这是它们最常用的方式。以下是可以处理重写条件的各种方式:

      <Pattern (is test string lower than pattern)
      >Pattern (is test string greater than pattern)
      =Pattern (is test string equal to pattern)
      -d (is test string a valid directory)
      -f (is test string a valid file)
      -s (is test string a valid file with size greater than zero)
      -l (is test string a symbolic link)
      -F (is test string a valid file, and accessible (via subrequest))
      -U (is test string a valid URL, and accessible (via subrequest))
      

      希望对你有帮助:)

      【讨论】:

      • 嗨,它已经适用于 Gorkem 的更改建议。还是您的代码更好? RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^\.]+)$ $1.php [NC,L]
      • [NC,L] - “标志”,告诉 Apache 如何应用规则。在这种情况下,我们使用了两个标志。 “NC”告诉 Apache 该规则不区分大小写,“L”告诉 Apache 如果使用该规则就不要再处理任何规则。
      • 请查看我更新的答案。它将帮助您了解规则是如何运作的。 :)
      猜你喜欢
      • 2011-04-30
      • 1970-01-01
      • 2014-03-04
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多