使用 htaccess 删除网站的 php 扩展 [英] Remove php extension of website with htaccess

查看:25
本文介绍了使用 htaccess 删除网站的 php 扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有这些链接:

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

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

http://crw4u.be/werkgever

使用此代码可以正常工作:

RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-f重写规则 ^([^\.]+)$ $1.php [NC,L]

但是我在我的 php 文件中使用了某些表单:

在 htaccess 中发送带有添加代码的表单时出现错误:

<块引用>

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

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

非常感谢!

托马斯

解决方案

试试这个规则:-

RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME}\.php -f重写规则 ^(.*)$ $1.php

以下是规则工作原理的基本描述:-

正则表达式

<预><代码>.(任何字符)*(前面的零或更多)+(上述一项或多项){}(最小到最大量词)?(非贪婪修饰符)!(在字符串的开头表示负模式")^(字符串的开头,如果在范围的开头则为负数")$(字符串结尾)[](匹配任何内容)- (范围如果在方括号之间使用)()(组,反向引用组)|(替代,或)\(转义字符本身)

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

标志

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

C(与下一条规则链接)CO=cookie(设置指定的cookie)E=var:value(将环境变量 var 设置为 value)F(禁止 - 向用户发送 403 标头)G(消失 - 不再存在)H=处理程序(设置处理程序)L(最后-停止处理规则)N(下一步 - 继续处理规则)NC(不区分大小写)NE(不要在输出中转义特殊的 URL 字符)NS(如果请求是子请求,则忽略此规则)P(代理 - 即 apache 应该获取替换部分中指定的远程内容并将其返回)PT(传递 - 在处理带有附加处理程序的 URL 时使用,例如 mod_alias)R(临时重定向到新 URL)R=301(永久重定向到新 URL)QSA(将请求中的查询字符串附加到替换的 URL)S=x(跳过下一个 x 规则)T=mime-type(强制指定 mime 类型)

例外和特殊情况

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

<Pattern(测试字符串低于模式)>Pattern(测试字符串是否大于模式)=Pattern(测试字符串是否等于模式)-d(测试字符串是否为有效目录)-f(测试字符串是否为有效文件)-s(测试字符串是大小大于零的有效文件)-l(测试字符串是否为符号链接)-F(测试字符串是否为有效文件,并且可访问(通过子请求))-U(测试字符串是一个有效的 URL,并且可以访问(通过子请求))

希望对你有帮助:)

I have these links on my website:

http://crw4u.be/werkgever.php (I can only add 2 urls in my question, since i'm a n00b)

I added a code to htaccess so I can access these pages without the php:

http://crw4u.be/werkgever

It works fine with this code:

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

Yet I use certain forms in my php files:

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

And I'm getting errors when sending the form with the added code in htaccess:

Not Found. The requested URL /email/ was not found on this server.

Does anyone knows how to adapt this code? I've been searching and testing, but can't seem to solve this.

Highly appreciated!

Tomas

解决方案

Try this Rule:-

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

Here is basic description for how rules are working:-

Regular Expressions

. (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)

Using regular expressions, it is possible to search for all sorts of patterns in URLs and rewrite them when they match

Flags

Flags are added to the end of a rewrite rule to tell Apache how to interpret and handle the rule. They can be used to tell apache to treat the rule as case-insensitive, to stop processing rules if the current one matches, or a variety of other options. They are comma-separated, and contained in square brackets. Here's a list of the flags, with their meanings.

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)

Exceptions and Special Cases

Rewrite conditions can be tested in a few different ways - they do not need to be treated as regular expression patterns, although this is the most common way they are used. Here are the various ways rewrite conditons can be processed:

<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))

Hope it will help you :)

这篇关于使用 htaccess 删除网站的 php 扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆