【问题标题】:htaccess clean URL's what's the best way to do it?htaccess clean URL 的最佳方法是什么?
【发布时间】:2012-09-21 14:16:12
【问题描述】:

我正在为我的网站制作干净的 Url,我注意到您在互联网上找到的内容几乎是关于将干净的 url 重写为您的服务器可以使用的 url。所以是这样的:

www.domain.com/profile/username --> www.domain.com/profile.php?u=username

RewriteEngine On
RewriteRule ^profile/(.*)$ /profile.php?u=$1 [L]

所以现在我应该在我的网站内链接到 cleanurl。但直观地说,链接到“脏”网址感觉更可靠/更好。因此,在这种情况下,您应该对上述内容使用重写器规则并使用重定向规则将“脏 url”重新定义为干净的 url,因此用户始终只能在浏览器中看到干净的 url。

www.domain.com/profile.php?u=username --> www.domain.com/profile/username

这是一件好事吗?是网站做的吗?反对/支持它的论据是什么?

你是怎么做这样的重定向的。像下面这样的东西似乎不起作用:

RewriteRule ^profile\.php?u=(.*)$ /profile/$1 [R=301, L]

我对 .htaccess 还很陌生,所以我可能会要求一些显而易见的东西......

【问题讨论】:

    标签: .htaccess mod-rewrite url-rewriting clean-urls


    【解决方案1】:
    RewriteRule ^profile\.php?u=(.*)$ /profile/$1 [R=301, L]
    

    这不起作用,因为您无法匹配来自 RewriteRule 的查询字符串(您的标志中还有一个杂散空间)。您还希望与实际请求而不是 URI 进行匹配,因为您只会导致来自其他规则的重定向循环。

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profile\.php\?u=([^&\ ]+)
    RewriteRule ^/?profile\.php$ /profile/%1 [R=301,L]
    

    【讨论】:

    • 好的,谢谢。但是像这样重写 url 和重定向 url 是个好主意吗?
    • @Juvlius 你这样做是为了让你无法控制的链接指向友好的链接。您在内容中生成的链接必须是友好的。您希望所有暴露在互联网上的 URL 都是友好的。您希望此重定向的唯一原因是指向不受您控制的任何旧链接(例如 Google 索引机器人)
    猜你喜欢
    • 1970-01-01
    • 2010-10-07
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 2010-12-16
    • 2012-10-07
    • 2013-04-21
    • 2015-10-08
    相关资源
    最近更新 更多