现代重写使用的.htaccess [英] Mod rewrite using .htaccess

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

问题描述

我想用这个资讯[从网站复制]重写我的模式,但它不工作..有什么不妥之处,PLZ给出一个正确的答案。

阿帕奇重写引擎主要用来把动态URL的如www.yoursite.com/product.php?id=123分为静态和用户友好的URL,如www.yoursite.com/product/123

  RewriteEngine叙述上
重写规则^产品/([^/.]+)/?$ product.php?ID = $ 1 [L]
 

另外一个例子,从改写:

www.yoursite.com/script.php?product=123到www.yoursite.com/cat/product/123 /

 重写规则猫/(.*)/(.*)/$ /script.php?$1=$2
 

解决方案

您似乎误会了重写模块(具体的规则)实际上没有。

很简单,当你浏览到:

 本地主机/ ABC / product.php?ID = 23
 

该重写规则不会被调用,不应该做任何事情。没有什么错在这里,你只是浏览到错误网址

URL转化

  http://www.yoursite.com/product/123<  -  URL的用户进入
http://www.yoursite.com/product.php?id=123<  - 重写后的URL,该服务器发现
 

重写规则(多个)explanined

一个重写规则分解成的的部件(不包括重写规则部分...)

  1. 在这对URL匹配的正则表达式
  2. 在它转变成网址
  3. 附加选项

由于您的规则:

 重写规则^产品/([^/.]+)/?$ product.php?ID = $ 1 [L]
 

正则表达式是: ^产品/([^/.]+)/?$
新的URL:<?code> product.php ID = $ 1
选项​​: [L]

这意味着用户浏览的不错的网址 http://www.yoursite.com/product/123 (和所有的您的链接指向的不错的网址),然后将服务器匹配对正则表达式并把它转换到新的URL只有服务器看到

实际上,这意味着你有两个网址指向同一页面...的不错的URL和未漂亮的 URL两者都将带你到同一个地方。所不同的是,未很好 / 标准的URL是来自公众和其他追求你的网站是隐藏的。


的正则表达式

为什么 http://mysite.com/product/image.jpg 不被重定向的原因是因为您在<$ C $使用正则表达式C>重写规则。

说明

  ^产品/([^/.]+)/?$
 

  • ^ =>开始串
  • 产品/ =>匹配的文本字符串产品/
  • ([^ /] +) => A捕获组匹配的一个或多个的字符,直到下一个 /
  • /?$ =>匹配一个可选的 / 后跟字符串的结尾

由于网址:

  http://mysite.com/product/image.jpg
 

您正则表达式匹配产品/图片但后来遇到停靠匹配的...至于说ISN 'T字符串的结尾 $ 规则是无效的,因此转换不会发生。

您可以通过改变你的性格类解决这个问题[^ /] 只是 [^ /] 或 - 我的 preferred 的方法 - 删除字符类,并简单地使用 + (鉴于你捕捉一切字符串的结尾。反正

 重写规则^产品/([^ /] +)/ $ product.php?ID = $ 1 [L]
重写规则^产品/(.+)/?$ product.php?ID = $ 1 [L]
 

I am trying to Rewrite my mode using this info[copy from a website], but it's not working .. what's wrong with it and plz give a correct answer..

The Apache rewrite engine is mainly used to turn dynamic url’s such as www.yoursite.com/product.php?id=123 into static and user friendly url’s such as www.yoursite.com/product/123

RewriteEngine on
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]

Another example, rewrite from:

www.yoursite.com/script.php?product=123 to www.yoursite.com/cat/product/123/

RewriteRule cat/(.*)/(.*)/$ /script.php?$1=$2

解决方案

You seem to have misunderstood what the rewrite module (and your rules specifically) actually does.

Quite simply when you browse to:

 localhost/abc/product.php?id=23

The RewriteRule isn't invoked and shouldn't be doing anything. There's nothing wrong here, you're just browsing to the wrong URL

URL Transformation

http://www.yoursite.com/product/123         <- URL that user goes to
http://www.yoursite.com/product.php?id=123  <- Rewritten URL that the server sees

RewriteRule(s) explanined

A rewrite rule is broken down into three parts (not including the RewriteRule part...)

  1. The regex that it matches against the url
  2. The url that it transforms into
  3. Additional options

Given your rule:

RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]

The regex is: ^product/([^/.]+)/?$
The new url : product.php?id=$1
The options : [L]

This means that the user browses to the nice url http://www.yoursite.com/product/123 (and all of your links point to the nice URLs) and then the server matches against the regex and transforms it to the new URL that only the server sees.

Effectively, this means that you have two URLs pointing to the same page... The nice URL and the not-nice URL both of which will take you to the same place. The difference is that the not-nice / standard URL is hidden from the general public and others pursuing your site.


Regex

The reason why http://mysite.com/product/image.jpg is not being redirected is because of the regex that you use in your RewriteRule.

Explanation

^product/([^/.]+)/?$

  • ^ => Start of string
  • product/ => Matches the literal string product/
  • ([^/.]+) => A capture group matching one or more characters up until the next / or .
  • /?$ => Matches an optional / followed by the end of the string

Given the URL:

http://mysite.com/product/image.jpg

Your regex matches product/image but then it encounters . which stops the matching... As that isn't the end of string $ the rule is invalidated and thus the transform never happens.

You can fix this by changing your character class [^/.] to just [^/] or - my preferred method - to remove the character class and simply use .+ (seeing as your capturing everything to the end of the string anyway

RewriteRule ^product/([^/]+)/?$ product.php?id=$1 [L]
RewriteRule ^product/(.+)/?$ product.php?id=$1 [L]

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

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