URL重写条件&参数

发布于 2024-12-28 00:58:10 字数 870 浏览 3 评论 0原文

----编辑---

我刚刚意识到我对问题的解释缺少一条重要的信息。

如果存在第二个参数,则 URL 应该被重定向。

因此,规则应为:

重定向任何包含 /d/ 的 URL,前提是在 URL 中也找到了 /d2/。

----结束编辑__

我需要将网站上包含特定参数的所有 URL 重定向到同一 URL,但还包含一个附加目录。所有需要重定向的 URL 都包含某个目录: /d/ 示例:

http://www.mysite.com/ category1/d/subcategory1/subdirectory2/

--应重定向至 --

http://www.mysite.com/newdirectory/category1/d/subcategory1/subdirectory2/

的共同点任何 URL 需要重定向的原因是它们都在 URL 中包含目录 /d/,该目录始终紧跟在“category”目录之后,如上面示例 URL 中以粗体所示的那样。然后,我想在类别目录前面插入一个附加目录,如上面示例 URL 中粗体所示。 URL 的其余部分将保持不变。

谁能帮忙解决这个问题吗?我对 mod_rewrite 比较陌生,并且意识到如果我做得不对,我可能会弄得一团糟。

预先感谢任何可以提供帮助的人

:)

----EDIT---

I have just realized that my explanation of the problem was missing an important piece of information.

The URL's should only be redirected if second parameter is present.

So the rule should read:

Redirect any URL that has /d/ in it, ONLY if /d2/ is also found in the URL.

----End Edit__

I have the need to 301 redirect all URL's on a site that contain a specific parameter to the same URL, but with an additional directory included. All of the URL's that require redirection contain a certain directory: /d/ Example:

http://www.mysite.com/category1/d/subcategory1/subdirectory2/

--Should Redirect to --

http://www.mysite.com/newdirectory/category1/d/subcategory1/subdirectory2/

The one thing in common with any of the URLs' requiring redirection is that they all contain a directory /d/ in the URL, which always immediately follows the "category" directory as indicated in bold in sample URL's above. I would then like to insert an additional directory in front of the category directory as indicated in bold in the sample URL's above. The rest of the URL will remain the same.

Can anyone help with this? I'm relatively new to mod_rewrite and realize I can make a big mess if I don't get it right.

Thanks in advance for anyone who can offer hlep

:)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

醉城メ夜风 2025-01-04 00:58:10

试试这个(编辑以反映有问题的变化):

    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} ^.*/d/.* [NC]
    RewriteCond %{REQUEST_URI} ^.*/d2/.* [NC]
    RewriteCond %{REQUEST_URI} !/newdirectory/ [NC]
    RewriteRule ^(.*)/d/(.*)$ http://www.mysite.com/newdirectory/$1/d/$2 [R=301,L]

Try this (edited to reflect change in question):

    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} ^.*/d/.* [NC]
    RewriteCond %{REQUEST_URI} ^.*/d2/.* [NC]
    RewriteCond %{REQUEST_URI} !/newdirectory/ [NC]
    RewriteRule ^(.*)/d/(.*)$ http://www.mysite.com/newdirectory/$1/d/$2 [R=301,L]
来世叙缘 2025-01-04 00:58:10

尝试将以下内容添加到站点根目录中的 htaccess 文件中。

RewriteEngine On
RewriteBase /

#if the url contains a /d2/ [NC]
RewriteCond %{REQUEST_URI} /d2/ [NC]
RewriteRule ^([-a-zA-Z0-9]+/d/[-a-zA-Z0-9]+/[-a-zA-Z0-9]+/)$ /newdirectory/$1 [L,R,NC]

Try adding the following to your htaccess file in the root directory of your site.

RewriteEngine On
RewriteBase /

#if the url contains a /d2/ [NC]
RewriteCond %{REQUEST_URI} /d2/ [NC]
RewriteRule ^([-a-zA-Z0-9]+/d/[-a-zA-Z0-9]+/[-a-zA-Z0-9]+/)$ /newdirectory/$1 [L,R,NC]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文