如何漂亮的查询字符串 URL

发布于 2024-12-12 07:29:43 字数 922 浏览 0 评论 0原文

我有一个基于查询字符串链接方案(即?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残月升风 2024-12-19 07:29:43

首先...重写仅适用于重写请求。因此,您问题中列出的更改现在将允许通过两种方式访问​​页面:

/index.php?page=about
/about/

这意味着,除非您在整个网站上进行更改,否则您不会真正进行太多更改,因为每个人都指向错误的 URL。

我认为您应该使用 mod_redirect 将用户重定向到新形成的 URL。我认为您可以将新的网址映射回您的网站实际期望的版本。我相信这有效,并且不会导致循环。

话虽这么说,我认为存在一些 SEO 问题,因为所有页面上都有重定向,而且没有人真正直接指向更好的 URL。这可能不会给你想要的结果。另一种选择是使用您提供的正则表达式,并在所有视图中实际更改真正的代码。这可能很容易也可能很困难,具体取决于您如何建立链接。

祝你好运。

澄清

我读了你的问题,因为你想要几个不同的东西:

  1. 你不想改变你网站的工作方式,但你想要漂亮的网址(也许你正在使用一个框架强制页面这样称呼)。这意味着您需要同时支持丑陋和漂亮的网址,这意味着您需要 mod_rewrite 以便两个版本都可以工作。
  2. 您的目标是为搜索引擎提供更好的网址。这意味着您应该“鼓励”使用丑陋 URL 的用户转而使用漂亮的 URL。在这种情况下,您可能应该清理网站上的旧网址。如果不是,谷歌将继续抓取丑陋的网址(因为这些将是它看到的唯一的网址)。
  3. 您无法清理其他人的 URL,因此您应该将他们指向丑陋 URL 的链接 mod_redirect 到您的好 URL。这样谷歌就会很好地找到好的网址。 (这是我不确定的部分。mod_redirect 和 mod_rewrite 会导致循环吗?我认为不会,但如果确实如此,那么只有 #1 和 #2 是可行的,你只需要与其他人们的网站指向您丑陋的网址

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:

/index.php?page=about
/about/

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:

  1. you don't want to change anything huge in the way your site works but you want nice URLs (perhaps you are using a framework forces pages to be called like this). This means that you need to support both ugly and nice urls, which means you need mod_rewrite so that both versions work.
  2. Your goal is to make better urls for search engines. That means that you should "encourage" users who use the ugly URLs to instead use the nice URLs. In that case you should probably clean up your old urls on your site. If not google will continue to crawl the ugly urls (since those will be the only ones it saw).
  3. You can't clean up other peoples URLs so you should probably mod_redirect their links to ugly urls to your nice ones. That way google will find the nice urls nicely. (this is the part i'm not sure of. Will the mod_redirect and mod_rewrite cause a loop? I think not, but if it does then only #1 and #2 would be doable, and you'd just need to live with other people's sites pointing to your ugly urls
滥情哥ㄟ 2024-12-19 07:29:43
RewriteEngine On

RewriteRule ^([a-zA-Z0-9_]+)$ /$1/ [NC,R=301]
RewriteRule ^([a-zA-Z0-9_]+)/$ /index.php?page=$1

RewriteRule ^(artists)/([a-zA-Z0-9_]+)$  /artists/$2/ [NC,R=301]
RewriteRule ^(artists)/([a-zA-Z0-9_]+)/$ /index.php?page=individual&i=$2

RewriteRule ^(photos)/([a-zA-Z0-9_]+)$  /photos/$2/ [NC,R=301]
RewriteRule ^(photos)/([a-zA-Z0-9_]+)/$ /index.php?page=photogallery&person=$2

RewriteRule ^(post)/([a-zA-Z0-9_]+)$  /post/$2/ [NC,R=301]
RewriteRule ^(post)/([a-zA-Z0-9_]+)/$ /index.php?page=post&id=$2
RewriteRule ^(rss)/([a-zA-Z0-9_]+)$  /rss/$2/ [NC,R=301]
RewriteRule ^(rss)/([a-zA-Z0-9_]+)/$ /index.php?page=post&rss=$2
RewriteRule ^(search)/([a-zA-Z0-9_]+)$  /search/$2/ [NC,R=301]
RewriteRule ^(search)/([a-zA-Z0-9_]+)/$ /index.php?page=post&q=$2

RewriteRule ^(video)/([a-zA-Z0-9_]+)$  /video/$2/ [NC,R=301]
RewriteRule ^(video)/([a-zA-Z0-9_]+)/$ /index.php?page=videogallery&bctid=$2
RewriteRule ^(playlist)/([a-zA-Z0-9_]+)$  /playlist/$2/ [NC,R=301]
RewriteRule ^(playlist)/([a-zA-Z0-9_]+)/$ /index.php?page=videogallery&bclid=$2

这是我最终使用的。不确定所有这些标志是否都是必要的......但它可以使用以前的代码。我仍然需要更改站点范围内的所有链接(前端和后端),但我还应该为所有旧链接添加重定向,以防我错过一个或其他网站链接到旧页面。

RewriteEngine On

RewriteRule ^([a-zA-Z0-9_]+)$ /$1/ [NC,R=301]
RewriteRule ^([a-zA-Z0-9_]+)/$ /index.php?page=$1

RewriteRule ^(artists)/([a-zA-Z0-9_]+)$  /artists/$2/ [NC,R=301]
RewriteRule ^(artists)/([a-zA-Z0-9_]+)/$ /index.php?page=individual&i=$2

RewriteRule ^(photos)/([a-zA-Z0-9_]+)$  /photos/$2/ [NC,R=301]
RewriteRule ^(photos)/([a-zA-Z0-9_]+)/$ /index.php?page=photogallery&person=$2

RewriteRule ^(post)/([a-zA-Z0-9_]+)$  /post/$2/ [NC,R=301]
RewriteRule ^(post)/([a-zA-Z0-9_]+)/$ /index.php?page=post&id=$2
RewriteRule ^(rss)/([a-zA-Z0-9_]+)$  /rss/$2/ [NC,R=301]
RewriteRule ^(rss)/([a-zA-Z0-9_]+)/$ /index.php?page=post&rss=$2
RewriteRule ^(search)/([a-zA-Z0-9_]+)$  /search/$2/ [NC,R=301]
RewriteRule ^(search)/([a-zA-Z0-9_]+)/$ /index.php?page=post&q=$2

RewriteRule ^(video)/([a-zA-Z0-9_]+)$  /video/$2/ [NC,R=301]
RewriteRule ^(video)/([a-zA-Z0-9_]+)/$ /index.php?page=videogallery&bctid=$2
RewriteRule ^(playlist)/([a-zA-Z0-9_]+)$  /playlist/$2/ [NC,R=301]
RewriteRule ^(playlist)/([a-zA-Z0-9_]+)/$ /index.php?page=videogallery&bclid=$2

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.

浅笑轻吟梦一曲 2024-12-19 07:29:43

我的意思是最好的方法是:

 RewriteRule ^([a-zA-z0-9-]+)/?([a-zA-Z0-9/-]+)?$ index.php?page=$1&par=$2

在 PHP 中:

 $page = $_GET["page"];
 $parm = split("/",$_GET["par"]);

 // first par
 $parm[0];

I mean the best way how do this is:

 RewriteRule ^([a-zA-z0-9-]+)/?([a-zA-Z0-9/-]+)?$ index.php?page=$1&par=$2

and in PHP:

 $page = $_GET["page"];
 $parm = split("/",$_GET["par"]);

 // first par
 $parm[0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文