简体   繁体   English

在.htaccess中为PHP文件创建重写规则

[英]Create a rewrite rule in .htaccess for php file

I am very new to .htaccess, I am having some problem with file action. 我对.htaccess非常陌生,我在执行文件操作时遇到了一些问题。 Example: 例:

http://www.domain.com/upload_pic.php?action=save_pic

I wrote the .htaccess rule like 我写了.htaccess规则像

RewriteEngine On
RewriteRule ^sabc([a-zA-Z0-9!@#$-_]*)$ upload_pic.php?action=$1

I want the desired result like: 我想要想要的结果,例如:

http://www.domain.com/sabc/save_pic

How can I get the desired result, please correct my .htaccess line. 如何获得所需的结果,请更正我的.htaccess行。

Change it to: 更改为:

RewriteEngine On
RewriteRule ^sabc/(.+)$ upload_pic.php?action=$1

The .+ will capture one or more characters after the / and be captured into $1 .+将在/之后捕获一个或多个字符,并捕获到$1

There are several other guides on the web already, but to understand it in better way as you are beginner @AMY , I have writen this for you. 网络上已经有其他几本指南,但是为了对初学者@AMY有更好的理解,我已为您编写了此指南。 Hope this will work for you. 希望这对您有用。

Most dynamic sites include variables in their URLs that tell the site what information to show the user. 大多数动态网站的URL中都包含变量,这些变量告诉该网站向用户显示哪些信息。 Typically, this gives URLs like the following, telling the relevant script on a site to load product number 7. 通常,这会提供如下所示的URL,告诉站点上的相关脚本加载产品编号7。

http://www.domain.com/upload_pic.php?pic_id=7

The problems with this kind of URL structure are that the URL is not at all memorable. 这种URL结构的问题在于URL一点都不值得记忆。 It's difficult to read out over the phone (you'd be surprised how many people pass URLs this way). 很难通过电话读出(您会惊讶地发现有如此多的人通过这种方式传递URL)。 Search engines and users alike get no useful information about the content of a page from that URL. 搜索引擎和用户都无法从该URL获得有关页面内容的有用信息。 You can't tell from that URL that that page allows you to buy a Norwegian Blue Parrot (lovely plumage). 您无法从该URL得知该页面允许您购买挪威蓝鹦鹉(可爱的羽毛)。 It's a fairly standard URL - the sort you'd get by default from most CMSes. 这是一个相当标准的网址-默认情况下,大多数CMS都会提供该网址。 Compare that to this URL: 将其与此URL进行比较:

http://www.domain.com/sabc/7/

Clearly a much cleaner and shorter URL. 显然,URL更简洁,更简短。 It's much easier to remember, and vastly easier to read out. 记住起来容易得多,读出起来也容易得多。 That said, it doesn't exactly tell anyone what it refers to. 就是说,它并不能完全告诉任何人它指的是什么。 But we can do more: 但是我们可以做更多的事情:

http://www.domain.com/sabc/user-navnish/

Now we're getting somewhere. 现在我们到了某个地方。 You can tell from the URL, even when it's taken out of context, what you're likely to find on that page. 即使从上下文中删除URL,也可以从URL知道在该页面上可能找到的内容。 Search engines can split that URL into words (hyphens in URLs are treated as spaces by search engines, whereas underscores are not), and they can use that information to better determine the content of the page. 搜索引擎可以将该URL分解为多个单词(URL中的连字符被搜索引擎视为空格,而下划线则不包含),并且他们可以使用该信息更好地确定页面的内容。 It's an easy URL to remember and to pass to another person. 这是一个易于记忆并传递给其他人的URL。

Unfortunately, the last URL cannot be easily understood by a server without some work on our part. 不幸的是,如果没有我们的帮助,服务器将不容易理解最后一个URL。 When a request is made for that URL, the server needs to work out how to process that URL so that it knows what to send back to the user. 当请求该URL时,服务器需要确定如何处理该URL,以便它知道向用户发送什么内容。 URL rewriting is the technique used to "translate" a URL like the last one into something the server can understand. URL重写是一种用于将URL(如最后一个URL)“翻译”成服务器可以理解的内容的技术。

To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. 为此,我们需要首先创建一个名为“ .htaccess”的文本文档来包含我们的规则。 It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). 必须准确命名(而不是“ .htaccess.txt”或“ rules.htaccess”)。 This would be placed in the root directory of the server (the same folder as "upload_pic.php" in our example). 这将放置在服务器的根目录中(在我们的示例中为与“ upload_pic.php”相同的文件夹)。 There may already be an .htaccess file there, in which case we should edit that rather than overwrite it. 可能已经有一个.htaccess文件,在这种情况下,我们应该编辑该文件而不是覆盖它。

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    ^sabc/?$    upload_pic.php    [NC,L]    # Handle requests for "sabc"

A couple of quick items to note - everything following a hash symbol in an .htaccess file is ignored as a comment, and I'd recommend you use comments liberally; 需要注意几个快速事项-.htaccess文件中的井号后面的所有内容都将被忽略为注释,我建议您自由使用注释。 and the "RewriteEngine" line should only be used once per .htaccess file (please note that I've not included this line from here onwards in code example). 并且“ RewriteEngine”行仅应在每个.htaccess文件中使用一次(请注意,在代码示例中,我从此处未包括此行)。

The "RewriteRule" line is where the magic happens. 发生魔术的地方是“ RewriteRule”行。 The line can be broken down into 5 parts: 该行可以分为5部分:

  • RewriteRule - Tells Apache that this like refers to a single RewriteRule. RewriteRule-告诉Apache这样的引用是指单个RewriteRule。

  • ^/sabc/?$ - The "pattern". ^ / sabc /?$-“模式”。 The server will check the URL of every request to the site to see if this pattern matches. 服务器将检查对站点的每个请求的URL,以查看此模式是否匹配。 If it does, then Apache will swap the URL of the request for the "substitution" section that follows. 如果是这样,那么Apache会将请求的URL交换为后面的“替代”部分。

  • upload_pic.php - The "substitution". upload_pic.php-“替代”。 If the pattern above matches the request, Apache uses this URL instead of the requested URL. 如果上述模式与请求匹配,则Apache使用该URL而不是请求的URL。

  • [NC,L] - "Flags", that tell Apache how to apply the rule. [NC,L]-“标志”,告诉Apache如何应用规则。 In this case, we're using two flags. 在这种情况下,我们使用两个标志。 "NC", tells Apache that this rule should be case-insensitive, and "L" tells Apache not to process any more rules if this one is used. “ NC”告诉Apache该规则不区分大小写,“ L”告诉Apache如果使用此规则,则不再处理任何规则。

# Handle requests for "sabc" - Comment explaining what the rule does (optional but recommended) #处理对“ sabc”的请求-注释说明规则的作用(可选,但建议)

The rule above is a simple method for rewriting a single URL, and is the basis for almost all URL rewriting rules. 上面的规则是重写单个URL的简单方法,并且是几乎所有URL重写规则的基础。

RewriteEngine On
RewriteRule ^sabc/(.+)$ upload_pic.php?action=$1

Hope this will be helpful for you to understand URL rewriting @AMY. 希望这对您了解URL重写@AMY有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM