【问题标题】:Redirect and rewrite the URL via mod_rewrite通过 mod_rewrite 重定向和重写 URL
【发布时间】:2012-02-22 22:25:29
【问题描述】:

我的主播是这样的

<a href="http://localhost/haveg/employer?id=7003&title=the-message-title">employer</a>

当我点击我想要到达的锚点时

http://localhost/haveg/employer/7003/the-message-title

到目前为止,我已经编写了这个 .htacess 代码

Options +FollowSymLinks
Options +Indexes
RewriteEngine on
RewriteBase /
RewriteRule ^haveg/employer/([0-9]+)/?(.*) haveg/employer.php?id=$1

只有当我输入 http://localhost/haveg/employer/7003/the-message-title 时才会打开页面,但我想在点击锚点后到达这里。

谢谢。

【问题讨论】:

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


    【解决方案1】:

    我想这就是你想要的:

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^haveg/employer/([0-9]+){1,5}/?(.*) haveg/employer.php?id=$1&title=$2 [L,NC]
    

    你的锚必须像这样构建:

    <a href="/haveg/employer/7003/the-message-title">employer</a>
    

    【讨论】:

    • 我不太明白。该代码不起作用,然后他告诉您更改链接,这样您就不需要重写了?
    • 链接需要匹配 URI,这就是他必须更改锚点的原因。重写工作,不知道你为什么说它没有
    • 对不起,我试图纠正我所说的但没有及时纠正......这是否意味着问题是倒退的。他想链接到 /haveg/employer/7003/the-message-title 并最终到达 /haveg/employer?id=7003&title=the-message-title。为什么 ?因为它表示前一个字符(斜杠)的零个或一个。不确定 {1,5} 是什么,为什么要限制位数?
    • 他想保持干净的 URI,因此通过了解 URI 结构 /haveg/employer/7003/the-message-title 重写只是在幕后进行,而不是进行适当的重定向。这样他就可以拥有干净的 URI,它也可以使用硬编码的方法工作。问号是他正在使用的,在不知道系统是否需要标题的情况下,允许问号很重要,因为尾部斜杠可以是可选的。 {1,5} 是限制重定向的好方法,但不是必需的。这只是个人喜好
    • 附带说明,我个人也不会使用 (.*)。我会将其限制为标题可接受的字符。即:[-a-z0-9]
    【解决方案2】:

    Mod 重写不会重定向您必须放置的链接
    http://localhost/haveg/employer/7003/the-message-title 在您的锚点中,mod rewrite 所做的是它将加载
    http://localhost/haveg/employer?id=7003&title=the-message-title 在后台显示它

    【讨论】:

      【解决方案3】:
      RewriteCond %{QUERY_STRING} ^id=([^&]*)\&title=([^&]*)$
      RewriteRule ^index.php /employer/%1/%2? [L,R=301]
      

      【讨论】:

      • 你能解释一下上面的内容吗?在我的情况下没有 index.php。
      • 显然我误解了你。我认为您需要在单击带有 url 参数的链接后跳转到带有斜杠分隔参数的页面。这里的 index.php 没关系,只是一个例子,不是生产就绪的解决方案,不是吗?
      猜你喜欢
      • 2011-04-14
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      • 2011-07-24
      • 2012-11-12
      • 1970-01-01
      • 2011-08-25
      • 2016-06-27
      相关资源
      最近更新 更多