友好的URL的.htaccess [英] Friendly Url .htaccess

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

问题描述

我试图遵循的.htaccess短期和友好的URL 的方向,但我无法取得这些成果。

I have tried to follow the directions on .htaccess short and friendly url but am unable to achieve these results.

如果我用下面的正常工作:

If I use the following it works fine:

RewriteEngine On
RewriteRule ^([^/]*)\.html$ /index.php?page=$1 [L]

不过,如果我使用:

However if I use:

RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]

我收到内部服务器错误。

I receive an internal server error.

所有我想要实现的是能够使用 http://website.com/about 代替的 http://website.com/about.html

All I am trying to achieve is to be able to use http://website.com/about instead of http://website.com/about.html

推荐答案

这些规则:

RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]

时引起内部重写循环。通过所有这些规则的重写引擎循环,直到内部重定向已达到限制(所以导致500服务器错误)或得到的URI出来的发动机是同一个走进它。另外(这是一个非常重要的概念),URI的斜线被删除的之前,它是在一个htaccess文件发送到重写引擎规则的。所以在这里,我们有:

Is causing an internal rewrite loop. The rewrite engine loops through all of the rules until either the internal redirect limit has been reached (thus resulting in a 500 Server error) or the resulting URI coming out of the engine is the same one that went into it. Additionally (and this is a very important concept), the leading slash of the URI is removed before it is sent to the rewrite engine for rules in an htaccess file. So here we have:

  1. 在提出要求: http://domain.com/blah
  2. URI = /等等
  3. 条斜线,URI = 等等
  4. 等等匹配 ^([^ /] *)$ ,应用规则,URI = /index.php?page =等等
  5. 等等不等于的index.php
  6. 在规则循环,URI = 的index.php
  7. 条斜线,URI = 的index.php
  8. 的index.php 匹配 ^([^ /] *)$ ,应用规则,URI = 的index.php ?页=的index.php
  9. 首页不等于的index.php
  10. 在规则循环,URI = 的index.php
  11. 条斜线,URI = 的index.php
  12. 的index.php 匹配 ^([^ /] *)$ ,应用规则,URI = 的index.php ?页=的index.php
  13. 首页不等于的index.php
  1. Request is made: http://domain.com/blah
  2. URI = /blah
  3. strip leading slash, URI = blah
  4. blah matches ^([^/]*)$, rule is applied, URI = /index.php?page=blah
  5. blah not equal /index.php
  6. rules loop, URI = /index.php
  7. strip leading slash, URI = index.php
  8. index.php matches ^([^/]*)$, rule is applied, URI = /index.php?page=index.php
  9. index not equal /index.php
  10. rules loop, URI = /index.php
  11. strip leading slash, URI = index.php
  12. index.php matches ^([^/]*)$, rule is applied, URI = /index.php?page=index.php
  13. index not equal /index.php

等。

您在如何处理这两个选择。该simples是简单地删除 / 从你的目标:

You have a couple of choices in how to deal with this. The simples would be to simply remove the / from your target:

RewriteRule ^([^/]*)$ index.php?page=$1 [L]

这将使它所以在上面的步骤9,在首页就等于的index.php ,从而停止重写的过程(之前它被用于第二次)。这个唯一的缺点是,如果你需要重定向,或者如果htaccess文件被移动,目标相对URI有时会被误认为是文件路径,而不是一个URL路径(阿帕奇猜测这一点,有时阿帕奇猜测错误)。这可以固定由包括基础

This would make it so in step 9 above, the index would equal index.php, thus stopping the rewrite process (before it gets applied the second time). The only downside with this is if you need to redirect, or if the htaccess file gets moved, the relative URI in the target can sometimes be mistaken for a file-path instead of a URL-path (apache guesses this, and sometimes apache guesses wrong). This can be fixed by including a base:

RewriteBase /
RewriteRule ^([^/]*)$ index.php?page=$1 [L]

您也可以添加条件,做起来很URL的指向现有的资源将排除该规则的人不适用:

You could also add conditions to make it so URL's that point to an existing resource will exclude this rule from getting applied:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]

因此​​,如果该URI指向现有文件或目录,该规则被跳过。因此,当URI成为第一个修改后的的index.php ,因为该文件存在,该规则将被跳过第2次左右。这也使得它如此的图片,CSS,脚本或静态页面的文档根目录的请求将不会得到通过路由的index.php 。如果这不是你想要的,你也可以平出跳过如果URI已经是一个的index.php:

So if the URI points to an existing file or directory, the rule is skipped. So after the first rewrite when the URI becomes /index.php, since that file exists, the rule is skipped the 2nd time around. This also makes it so requests for images, css, scripts or static pages in your document root won't get routed through index.php. If this isn't what you want, you can also flat-out skip if the URI is already an index.php:

RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]

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

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