Mod rewrite using .htaccess(使用 .htaccess 重写 Mod)
问题描述
我正在尝试使用此信息[从网站复制]重写我的模式,但它不起作用..有什么问题,请给出正确答案..
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..
Apache重写引擎主要用于将www.yoursite.com/product.php?id=123等动态url转为www.yoursite.com/product/123等静态且用户友好的url
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]
另一个例子,重写自:
www.yoursite.com/script.php?product=123 到 www.yoursite.com/cat/product/123/
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.
很简单,当您浏览到:
localhost/abc/product.php?id=23
RewriteRule 没有被调用,也不应该做任何事情.这里没有任何问题,您只是浏览了错误 URL
The RewriteRule isn't invoked and shouldn't be doing anything. There's nothing wrong here, you're just browsing to the wrong URL
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
部分...)
RewriteRule(s) explanined
A rewrite rule is broken down into three parts (not including the RewriteRule
part...)
- 它与 url 匹配的正则表达式
- 它转换成的网址
- 其他选项
根据您的规则:
RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L]
正则表达式为:^product/([^/.]+)/?$
新网址:product.php?id=$1
选项:[L]
这意味着用户浏览到 nice url http://www.yoursite.com/product/123
(并且您的所有链接都指向 nice URL),然后服务器与正则表达式匹配并将其转换为只有服务器可以看到的新 URL.
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.
实际上,这意味着您有两个 URL 指向同一页面...nice URL 和 not-nice URL 都将带您到同一个地方.不同之处在于,不太好/标准 URL 对普通公众和其他访问您网站的人是隐藏的.
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.
http://mysite.com/product/image.jpg
没有被重定向的原因是因为您在 RewriteRule
中使用了正则表达式.
The reason why http://mysite.com/product/image.jpg
is not being redirected is because of the regex that you use in your RewriteRule
.
^product/([^/.]+)/?$
^
=> 字符串开始product/
=> 匹配文字字符串product/
([^/.]+)
=> 匹配一个或多个字符直到下一个/
或的捕获组>.
/?$
=> 匹配可选的/
后跟字符串的结尾^
=> Start of stringproduct/
=> Matches the literal stringproduct/
([^/.]+)
=> A capture group matching one or more characters up until the next/
or.
/?$
=> Matches an optional/
followed by the end of the string
给定网址:
http://mysite.com/product/image.jpg
您的正则表达式匹配 product/image
但随后遇到 .
这会停止匹配...因为那不是字符串 $的结尾code> 规则无效,因此转换永远不会发生.
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 重写 Mod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 .htaccess 重写 Mod
基础教程推荐
- HTTP 与 FTP 上传 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01