怎样的.htaccess工作? [英] How does .htaccess work?

查看:113
本文介绍了怎样的.htaccess工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的网站上显示的其他页面作为www.example.com/pageone/链接,而不是www.example.com/pageone.html。

I'm trying to make my website display the other pages as a www.example.com/pageone/ link instead of www.example.com/pageone.html.

问题是,我对如何做到这一点使用.htaccess文件和它让我困惑,因为我不明白的命令读了。

Problem is, i'm reading up on ways to do that using .htaccess and its getting me confused because i don't understand the commands.

据我所知,第一个步骤,但是,在.htaccess文件写这篇文章:

I understand that the first step, however, is to write this in the .htaccess file:

RewriteEngine On

这一步之后,我完全不知道什么!-d也不{} REQUEST_FILENAME ^也没有(。*)和所有节目的文字。有没有我可以参考的文档?

After this step, i have absolutely no idea whats !-d nor {REQUEST_FILENAME} nor ^(.*) and all that programming text. Is there a documentation that i can refer to?

或者任何人都可以提供我如何.htaccess文件配置明白一个简单的解释,如果我想去

Or can anyone provide me a simple explanation on how to configure the .htaccess file to understand that if i want to go to

www.example.com/pageone.html

,我需要键入的URL为

, all i need to type into the URL is

www.example.com/pageone/

和PHP文件等呢?

推荐答案

首先,有在官方文档 。为了解决您的具体问题,我会去这样的:

First of all, there's the Official Documentation. To solve your specific problem, I would go about this way:

RewriteEngine on #Turn on rewrite

RewriteCond %{REQUEST_FILENAME} !-f #If requested is not a filename...
RewriteCond %{REQUEST_FILENAME} !-d #And not a directory
RewriteRule ^([^/]+)/?$ /$1.html [L] #Preform this redirect

的RewriteCond 唯一适用于翌年规则。如果你有多个规则,你需要编写条件各一个。

The RewriteConds only apply to the next following rule. If you were to have multiple rules, you'd need to write the conditions for each one.

现在,Apache服务器请求的路径(一切www.example.com/后)匹配,看它是否匹配任何您指定的规则。在这种情况下,只有一个:

Now, the Apache server matches the requested path (everything after www.example.com/), to see if it matches any of the rules you've specified. In which case, there is only one:

^([^/]+)$

这经常EX pression匹配任何数目的字符,这是不削减 / ,其次是可选尾随斜线。如果找到匹配,这将改写请求到第二个参数: / $ 1.HTML $ 1 表示无论是在括号,这在我们的情况是所有非斜杠字符。之间是否匹配

This regular expression matches any number of characters, which are not slash /, followed by an optional trailing slash. If the match was found, it will rewrite the request to the second parameter: /$1.html, $1 means "Whatever was matched between the brackets", which in our case is all of the non-slash characters.

[L] 标志,告诉重写引擎停止寻找规则,如果该规则被匹配。

The [L] flag, tells the rewriting engine to stop looking for rules if this rule was matched.

因此​​得出结论, www.example.com/ 什么/ 将在服务器sliently重写 WWW .example.com的/ 任何的.html

So to conclude, www.example.com/whatever/ will be rewritten sliently at the server to www.example.com/whatever.html

这篇关于怎样的.htaccess工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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