【问题标题】:RewriteRule and passed variables not workingRewriteRule 和传递的变量不起作用
【发布时间】:2013-09-06 23:45:16
【问题描述】:

我正在努力使我的网站对 SEO 更友好。我研究了这个和其他几个网站,但基本上没有一个解决方案有效。使用这个简单的代码可以很好地重写 URL:

RewriteRule ^testpage\.php$ destination.php [NC,L]    

但是,当我尝试在动态页面上实现 RewriteRule 并在 URL 中传递变量时,什么也没有发生。这是我现在无法使用的代码:

RewriteRule ^([^/]*)\.html$ /prodinfo.php?prodid=$1 [NC,L]

我想要http://www.mysite.com/products/prodinfo.php?id=268 转换为http://www.mysite.com/268.html

我在 Apache 2.2 和 php.5.3 谢谢。

【问题讨论】:

  • 什么不起作用?你看过 RewriteLog 的输出吗?

标签: apache mod-rewrite url-rewriting


【解决方案1】:

你正在寻找这样的东西吗?

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /products/prodinfo\.php\?id=([0-9]+)
RewriteRule ^ /%1.html? [L,R=301]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+).html$ /products/prodinfo.php?id=$1 [L]

【讨论】:

  • 是的。非常感谢。
【解决方案2】:

在浏览器中访问:http://example.com/268.html 的任何人都已将其在服务器端转换为:http://example.com/products/prodinfo.php?id=268,其中:

RewriteEngine On
#RewriteRule ^([0-9]+).html/?$ /products/prodinfo.php?id=$1 [NC]

...或者相反,将访问者的请求翻译为:http://example.com/products/prodinfo.php?id=268http://example.com/268.html 在他们的浏览器使用中:

RewriteEngine On
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^products/prodinfo.php$ /%1.html? [R]

【讨论】:

  • 谢谢,但这不是我所需要的。您的解决方案重定向到 http:example.com/268.html,最终出现 404 错误。我正在尝试将 URL 重写为 http:example.com/268.html。 Baiscally,我正在尝试执行以下操作generateit.net/mod-rewrite
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-12
  • 2023-03-09
相关资源
最近更新 更多