【问题标题】:htaccess RewriteRule works but browser does not display friendly URLhtaccess RewriteRule 工作,但浏览器不显示友好的 URL
【发布时间】:2013-07-27 04:25:19
【问题描述】:

我只想拥有 URL:

example.com/connect/admin/create

重定向到:

example.com/connect/admin/create.php

我在 .htaccess 文件中有以下代码:

RewriteEngine On
RewriteBase /
RewriteRule ^connect/admin/create$ http://example.com/connect/admin/create.php [R=301,L,NE]

并且它会正确重定向。问题是浏览器现在显示

example.com/connect/admin/create.php

当我仍然希望它显示时:

example.com/connect/admin/create

我需要向 .htaccess 添加其他内容吗?

谢谢

【问题讨论】:

  • 这是因为您通过R=301 进行了硬重定向。如果您不希望它重定向浏览器,只需将其删除,它就会静默重写。另外,您需要删除http://example.com/
  • 根据你的回复,我把代码改成:RewriteRule ^connect/admin/create$ /connect/admin/create.php [L,NE] 结果还是一样。
  • 您还需要从/connect/admin/create.php 中删除前导/ 绝对路径会导致它尝试重定向。
  • 它实际上已被缓存在浏览器中,无论出于何种原因。我清除了缓存,它起作用了。
  • 好的,我把它放在下面作为答案

标签: .htaccess url-rewriting friendly-url


【解决方案1】:

您的原始重写规则包含通过R=301 的显式重定向。该标志将需要删除。其次,RewriteRule(目标)的右侧包括http://example.com/,这也将隐式强制浏览器重定向。相反,只使用右侧的相对路径:

RewriteEngine On
RewriteBase /
RewriteRule ^connect/admin/create$ connect/admin/create.php [L,NE]

如果您还想在 connect/admin 中删除您想要删除 .php 的其他操作,则可以以更通用的方式完成此操作:

# Capture the action into $1 and pass it to the redirect target.
RewriteRule ^connect/admin/([^/.]+)/?$ connect/admin/$1.php [L,NE]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-16
    • 2016-07-22
    • 2014-01-06
    • 1970-01-01
    • 2014-08-16
    • 1970-01-01
    • 2014-01-25
    • 2012-02-02
    相关资源
    最近更新 更多