我有一个 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.php
和 pages/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!
发布评论
评论(1)
在某种程度上,你问了错误的问题。您给出的规则有效并且可以实现您想要的效果。您的问题是脚本正在为您的 CSS、图像等发出相对地址,因此当客户端浏览器请求包含相对链接的
example.com/en/about/topic
时href= “base/code/view.css”
,客户端会将其转换为鉴于您不想更改脚本,因此您面临的挑战是识别这种类型的模式并去掉 SEO 编码引入的无关目录,所以如果这种情况是
example.com/en//base/code/...
那么以下类型的规则将执行所需的操作替换
/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 linkhref="base/code/view.css"
, the client will convert this toGiven 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 wantReplace
/base/code
by whatever match patterns you need.