【问题标题】:mod_rewrite not working in htaccessmod_rewrite 在 htaccess 中不起作用
【发布时间】:2014-05-29 16:13:07
【问题描述】:

在我的目录根目录下的 .htaccess 文件中,我有这个;

RewriteEngine on
RewriteBase /

RewriteRule ^website-comments/webid/([^/]*)$ /website-comments?webid=$1 [L]

但由于某种原因,它没有重写 URL,如果我的想法是正确的,那么如果用户访问 www.website.com/website-cmets?webid=1 它应该将 URL 更改为 www.website。 com/website-cmets/webid/1 但它似乎没有工作,我做错了什么吗?提前致谢

编辑 1

其他 HTACCESS 规则

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

【问题讨论】:

  • 在你的服务器上,最终应该执行的脚本是/website-comments?webid=$1? mod_rewrite 不会自动将直接指向该资源的链接更改为您首选的漂亮 URL - 您需要编写一个规则来首先重定向到漂亮 URL。请通过示例输入 URL、输出 URL 以及最终执行请求的脚本进一步阐明您的需求。
  • @MichaelBerkowski 因此,如果我只是在 URL 中输入类似 website-cmets/webid/1 之类的内容,它应该可以工作,但如果我尝试通过单击链接访问它,它就不会重定向?跨度>
  • 使用您当前的规则,访问 URL website-comments/webid/1 将静默重写以提供 ?webid=1。但是,如果用户点击了一个旧的、不漂亮的链接,该链接直接指向/website-comments?webid=1,那么他最终会看到的 URL。您需要 another 规则首先将其重定向到 /website-comments/webid/1 然后像它已经做的那样默默地提供脚本。您的目标是最终用户永远不会看到/website-comments?webid=1
  • @MichaelBerkowski 啊哈我明白了!非常感谢,如果您将其写成答案,我会为您接受
  • @user3263978 好的,我想我已经制定了您需要的以下规则。

标签: php .htaccess url mod-rewrite rewrite


【解决方案1】:

Apache 的 mod_rewrite 与基本规则一起使用时可以静默地为您的用户提供内部 URL,但它不能强制使用相同的简单规则使用该内部 URL。通常,如果您希望最终用户从不使用实际 URL(在您的情况下为 /website-comments?webid=123),您需要首先提供一个规则,通过在原始 HTTP 请求中匹配它来重定向该 URL,使用Apache的变量%{THE_REQUEST}

RewriteEngine On
# probably don't actually need RewriteBase
RewriteBase /

# First match the URL which the user requested. If it is ?webid=N
# do an actual redirection to the pretty URL
# This will grab digits from the query string to use in %1
RewriteCond %{THE_REQUEST} webid=(\d+)
# ...only doing this for website-comments -- a permanent redirect to the pretty URL
# so the client makes a new request to the pretty URL that won't match the above condition 
# on the next trip.
#
# Since the [R] will automatically append the old query string, add a ?
# onto the end to clear out the query string so this rule doesn't match again and
# go into an infinite loop.
RewriteRule website-comments /website-comments/webid/%1? [L,R=301]

# Then apply your existing rule to rewrite internally to the target resource
RewriteRule ^website-comments/webid/([^/]*)$ /website-comments?webid=$1 [L]

上面的(\d+) 假设webid 将只是一个整数。如果这不是真的并且它可能包含其他字符,请考虑使用 ([^&]+) 来匹配直到下一个 & 或字符串结尾的所有内容。

【讨论】:

  • 在末尾写着 [L,R=301] 的那一行,应该是 1 美元吗?还是实际上是 %1?
  • 其实是%1()RewriteRule 中匹配的组使用$,但在RewriteCond 中匹配的组使用%
  • 好的,我已经完成了,但现在我的网站出现 500 内部服务器错误
  • 好吧,我想我找到了。 [R=301] 自动附加原始查询字符串,因此webid=123 被发送到后续重定向,从而产生/website-comments/webid/123?webid=123。这再次匹配 THE_REQUEST 并进入循环。解决方案是在/website-comments/webid/%1? 上使用? 清除原始查询字符串它在我的测试中有效。
  • 好吧有道理!我已经改变了,但我仍然收到错误
【解决方案2】:

如果我的想法是正确的,那么如果用户访问 www.website.com/website-cmets?webid=1 它应该将 url 更改为 www.website.com/website-cmets/webid/1

没有。 incoming URI “来自野外”(由用户输入,由书签或搜索引擎提供,或您网站中的链接)将是 SEO 形式:/website-comments/webid/1。 .htaccess 的工作是使用查询字符串将其更改为 dynamic 格式,您的 PHP 脚本可以消化(读取 $_GET 中的元素):/website-comments.php?webid=1。所以,你把它倒过来了。

RewriteEngine On
RewriteRule  ^website-comments/webid/([0-9]+)/?$  /website-comments.php?webid=$1 [L]

这将是一种方法,并且可以满足您的需求。

【讨论】:

    【解决方案3】:

    试试这个(切换)RewriteRule 参数

    RewriteRule ^/website-comments?webid=([^/]*)$ website-comments/webid/$1[L]
    

    【讨论】:

    • 不,没有任何改变
    • 查询字符串永远不会传递到RewriteRule,因此左侧永远不会在这里匹配。必须在RewriteCond处理。
    猜你喜欢
    • 2013-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-30
    • 2017-06-12
    • 2010-10-31
    • 2011-07-31
    相关资源
    最近更新 更多