【问题标题】:RewriteRule is creating new URLs but not redirecting them from the dynamic onesRewriteRule 正在创建新 URL,但不会从动态 URL 重定向它们
【发布时间】:2015-06-05 06:37:07
【问题描述】:

我的网站http://www.musicea.com.au 有动态网址。我使用 .htaccess 重写了静态 URL。当我复制重写的静态 URL 并直接粘贴到它们打开的浏览器中时。但他们不会自动重定向。我的代码如下所示。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page-([0-9]+)\$ content.php?pageid=$1 [L]
RewriteRule ^$ content.php?pageid=home
RewriteRule ^about$ content.php?pageid=1341369117 [L,NC]
RewriteRule ^music-lessons$ content.php?pageid=1344314997 [L,NC]
RewriteRule ^piano-lessons$ content.php?  pageid=1347926925&getsubnavid=1344314997 [L,NC]
RewriteRule ^keyboard-lessons$ content.php?pageid=1347927089&getsubnavid=1344314997 [L,NC]
RewriteRule ^guitar-lessons$ content.php?pageid=1347927135&getsubnavid=1344314997 [L,NC]
RewriteRule ^violin-lessons$ content.php?pageid=1347927198&getsubnavid=1344314997 [L,NC]
RewriteRule ^content.php?pageid=home$ http://www.musicea.com.au/ [L,NC]

我认为 URL 已正确重写,但重定向存在问题。 请给我建议。

【问题讨论】:

  • 哪些不起作用,您是如何在浏览器中输入它们的?我可以告诉你最后一个不会,因为 rewriterule 忽略了模式中的查询字符串。在您的第一条规则中,也不要逃避 $。
  • 我重写的所有东西都是手动工作的。例如,当我手动输入musicea.com.au/music-lessons 时,URL 会打开。但它不会自动从动态重定向:musicea.com.au/content.php?pageid=1344314997。请忽略最后一个。我会删除它。
  • 那是因为你没有规则告诉它这样做。您必须创建一个规则,以便如果他们输入旧 URL,它将重定向到新 URL。
  • 谢谢。你能告诉我我该怎么做吗?这将是很大的帮助。

标签: .htaccess mod-rewrite url-rewriting url-redirection


【解决方案1】:

这是我在 cmets 中的意思的一个例子。您只有一条规则可以在内部重写到您的 PHP 文件,但您没有一条规则可以直接重定向 php 文件的使用。因此,我将向您展示如何做到这一点的示例。我做了一个规则,以便您可以看到。但是因为您的所有 URI 都没有到您的动态 URL 的任何映射,所以您必须为每个规则执行类似的规则。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page-([0-9]+)\$ content.php?pageid=$1 [L]
RewriteRule ^$ content.php?pageid=home
RewriteRule ^about$ content.php?pageid=1341369117 [L,NC]

#redirect direct request for content.php for music lesson
RewriteCond %{THE_REQUEST} [A-Z]{3,}\ /content\.php\?pageid=1344314997
RewriteRule ^ /music-lessons? [R=301,L]
RewriteRule ^music-lessons$ content.php?pageid=1344314997 [L,NC]

RewriteRule ^piano-lessons$ content.php?  pageid=1347926925&getsubnavid=1344314997 [L,NC]
RewriteRule ^keyboard-lessons$ content.php?pageid=1347927089&getsubnavid=1344314997 [L,NC]
RewriteRule ^guitar-lessons$ content.php?pageid=1347927135&getsubnavid=1344314997 [L,NC]
RewriteRule ^violin-lessons$ content.php?pageid=1347927198&getsubnavid=1344314997 [L,NC]

如果这有帮助,请告诉我。

【讨论】:

  • 谢谢杰克。有用。这确实是一个很大的帮助。感谢您的宝贵时间。
猜你喜欢
  • 1970-01-01
  • 2015-05-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多