SEO mod_rewrite内部uri目录问题

发布于 2024-12-27 07:46:10 字数 2033 浏览 1 评论 0 原文

我有一个 mod_rewrite 规则,将 example.com/en/about/topic 更改为 example.com/en/view.php?p=pages/about/topic.php

我的规则是:

RewriteRule ^en/([a-zA-Z0-9/]+)$ en/view.php?p=pages/$1.php

view.php 包含我所有持久的东西,如菜单、徽标等,它使用 php include获取pages/topic.php

它适用于 example.com/en/topic -> example.com/en/view.php?p=pages/topic.php 但是当我尝试 example.com/en/about/topic -> example.com/en/view.php?p=pages/about/topic.php,它找到 view.phppages/about/topic.php > 并加载它们,但尝试从 en/about/... 而不是 en/... 加载我所有的 css 文件和图像等。它对图像执行此操作在 view.php 和 topic.php 中。最令人沮丧的是,如果我在规则中添加 [R],一切正常,但它违背了 SEO 练习的目的!

我一整天都在做这个,但没有运气。虽然我确实发现了 mod_rewrite 日志记录和一些方便 指南

这是我的日志在 example.com/en/test 中的内容(有效):

[perdir C:/tt/xampp/htdocs/] 重写 'en/test' -> 'en/view.php?p=pages/test.php'

[perdir C:/tt/xampp/htdocs/] 去除 document_root 前缀: C:/tt/xampp/htdocs/en/view.php -> /en/view.php

[perdir C:/tt/xampp/htdocs/] 内部重定向 /en/view.php [内部重定向]

[perdir C:/tt/xampp/htdocs/] 通过 C:/tt/xampp/htdocs/en/view.php

[perdir C:/tt/xampp/htdocs/] 通过 C:/tt/xampp/htdocs/en/base/code/view.css

和 example.com/en/about/test (没有)

[perdir C:/tt/xampp/htdocs/] 重写 'en/about/dir/test' -> 'en/view.php?p=pages/about/dir/test.php'

[perdir C:/tt/xampp/htdocs/] 去掉 document_root 前缀: C:/tt/xampp/htdocs/en/view.php -> /en/view.php

[perdir C:/tt/xampp/htdocs/] 内部重定向 /en/view.php [内部重定向]

[perdir C:/tt/xampp/htdocs/] 通过 C:/tt/xampp/htdocs/en/view.php

[perdir C:/tt/xampp/htdocs/] 通过 C:/tt/xampp/htdocs/en/about

任何帮助将不胜感激!

I have a mod_rewrite rule that changes example.com/en/about/topic to example.com/en/view.php?p=pages/about/topic.php

My rule is:

RewriteRule ^en/([a-zA-Z0-9/]+)$ en/view.php?p=pages/$1.php

view.php has all my persistent stuff like menus, logo etc and it uses a php include to get pages/topic.php

It works on example.com/en/topic -> example.com/en/view.php?p=pages/topic.php but when I try example.com/en/about/topic -> example.com/en/view.php?p=pages/about/topic.php, it finds view.php and pages/about/topic.php and loads them but tries to load all my css files and images etc. from en/about/... instead of en/... It does this for images in both view.php and topic.php. The most frustrating thing is that if I add [R] to the rule, everything works fine but it defeats the purpose of the SEO exercise!

I’ve been at this all day but no luck. Though I did discover mod_rewrite logging and a couple of handy guides.

Here’s what my log says for example.com/en/test (which works):

[perdir C:/tt/xampp/htdocs/] rewrite 'en/test' ->
'en/view.php?p=pages/test.php'

[perdir C:/tt/xampp/htdocs/] strip document_root prefix:
C:/tt/xampp/htdocs/en/view.php -> /en/view.php

[perdir C:/tt/xampp/htdocs/] internal redirect with /en/view.php
[INTERNAL REDIRECT]

[perdir C:/tt/xampp/htdocs/] pass through
C:/tt/xampp/htdocs/en/view.php

[perdir C:/tt/xampp/htdocs/] pass through
C:/tt/xampp/htdocs/en/base/code/view.css

and for example.com/en/about/test (which does not)

[perdir C:/tt/xampp/htdocs/] rewrite 'en/about/dir/test' ->
'en/view.php?p=pages/about/dir/test.php'

[perdir C:/tt/xampp/htdocs/] strip document_root prefix:
C:/tt/xampp/htdocs/en/view.php -> /en/view.php

[perdir C:/tt/xampp/htdocs/] internal redirect with /en/view.php
[INTERNAL REDIRECT]

[perdir C:/tt/xampp/htdocs/] pass through
C:/tt/xampp/htdocs/en/view.php

[perdir C:/tt/xampp/htdocs/] pass through C:/tt/xampp/htdocs/en/about

Any help would be much appreciated!

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

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

发布评论

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

评论(1

我一直都在从未离去 2025-01-03 07:46:10

在某种程度上,你问了错误的问题。您给出的规则有效并且可以实现您想要的效果。您的问题是脚本正在为您的 CSS、图像等发出相对地址,因此当客户端浏览器请求包含相对链接的 example.com/en/about/topichref= “base/code/view.css”客户端会将其转换为

example.com/en/about/base/code/view.css

鉴于您不想更改脚本,因此您面临的挑战是识别这种类型的模式并去掉 SEO 编码引入的无关目录,所以如果这种情况是example.com/en//base/code/... 那么以下类型的规则将执行所需的操作

RewriteRule en/.+?/base/code/(.*) en/base/code/$1   [L]

替换 /base/code 通过您需要的任何匹配模式。

In a way you are asking the wrong question. The rule that you give works and does what you want. Your problem is that the scripts are issuing relative addresses for your CSS, images, etc. so when the client browser requests example.com/en/about/topic which contains a relative link href="base/code/view.css", the client will convert this to

example.com/en/about/base/code/view.css

Given that you don't want to change your scripts, your challenge is therefore to recognise this type of pattern and strip out the extraneous directories that the SEO encoding introduces, so if this case is example.com/en/<don't care>/base/code/... then the following type of rule will do what to want

RewriteRule en/.+?/base/code/(.*) en/base/code/$1   [L]

Replace /base/code by whatever match patterns you need.

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