如何漂亮的查询字符串 URL
我有一个基于查询字符串链接方案(即?page=about 或?page=individual&i=johndoe)构建的整个网站。当然,回想起来,我们决定采用不同的(美化的)方案,以便对 SEO 更加友好(即 /about/ 或 /individual/johndoe/)。
有没有办法在 Apache .htaccess 文件上使用 mod_rewrite 来完成此更改,而无需更改站点范围内的所有链接?例如,如果您单击指向 ?page=about 的链接,它会将您永久重定向到 /about/。
我尝试过的代码将成功地将 /about/ 显示为 ?page=about,但是,不涉及重定向。老实说,我从未在 mod_rewrite 中做过任何工作(这有点令人生畏),所以我觉得我走错了方向。尽管如此,这是我迄今为止一直在使用的代码:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_]+)$ /$1/ [R]
RewriteRule ^([a-zA-Z0-9_]+)/$ /index.php?page=$1
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$ /$1/$2/ [R]
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&i=$2
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&id=$2
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&bctid=$2
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&bclid=$2
有什么想法吗?我非常感谢任何帮助。
I have an entire website built upon a link scheme of query strings (i.e. ?page=about or ?page=individual&i=johndoe). Of course, in retrospect we have decided to go with a different (beautified) scheme in order to be more SEO friendly (i.e. /about/ or /individual/johndoe/).
Is there a way to accomplish this change using mod_rewrite on an Apache .htaccess file without having to change all the links sitewide? For instance, if you click a link to ?page=about it would permanently redirect you to /about/.
The code I have tried will successfully display /about/ as ?page=about, however, there is no redirect involved. And to be honest, I've never done any work in mod_rewrite (and it's a bit intimidating), so I feel I'm going in the wrong direction. Nonetheless, here's the code I've been working with so far:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_]+)$ /$1/ [R]
RewriteRule ^([a-zA-Z0-9_]+)/$ /index.php?page=$1
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$ /$1/$2/ [R]
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&i=$2
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&id=$2
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&bctid=$2
RewriteRule ^([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/$ /index.php?page=$1&bclid=$2
Any thoughts? I greatly appreciate any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先...重写仅适用于重写请求。因此,您问题中列出的更改现在将允许通过两种方式访问页面:
这意味着,除非您在整个网站上进行更改,否则您不会真正进行太多更改,因为每个人都指向错误的 URL。
我认为您应该使用 mod_redirect 将用户重定向到新形成的 URL。我认为您可以将新的网址映射回您的网站实际期望的版本。我相信这有效,并且不会导致循环。
话虽这么说,我认为存在一些 SEO 问题,因为所有页面上都有重定向,而且没有人真正直接指向更好的 URL。这可能不会给你想要的结果。另一种选择是使用您提供的正则表达式,并在所有视图中实际更改真正的代码。这可能很容易也可能很困难,具体取决于您如何建立链接。
祝你好运。
澄清
我读了你的问题,因为你想要几个不同的东西:
First... The rewrite will only apply to rewriting requests. As a result your changes listed in your questions will now allow a page to be accessed in two ways:
That means that unless you make changes throughout your site you will not really be making much of a change since everyone is pointing to the wrong URL.
I think instead you want to use mod_redirect, to redirect the user to the newer formed URL. I think you can then have that new url get mapped back to the version your site actually expects. I believe that this works, and doesn't cause a loop.
That being said i think there is some SEO ding since there is a redirect on all pages, and no one actually points to the nicer URLs directly. That might not give you the results you want. Another option would be to use those regex that you provide, and actually make the real code change in all your views. That might be easy or hard depending on how you are making your links.
Good luck.
Clarification
I read your questions as you want several different things:
这是我最终使用的。不确定所有这些标志是否都是必要的......但它可以使用以前的代码。我仍然需要更改站点范围内的所有链接(前端和后端),但我还应该为所有旧链接添加重定向,以防我错过一个或其他网站链接到旧页面。
Here's what I ended up using. Not sure if all those flags are necessary... but it works using the previous code. I still need to change all links sitewide (front-end and back-end), but I should also put in redirects for all the old links incase I miss one or in case other websites link to the old pages.
我的意思是最好的方法是:
在 PHP 中:
I mean the best way how do this is:
and in PHP: