mod_rewrite 页面美化

发布于 2024-09-02 02:09:09 字数 614 浏览 2 评论 0原文

所以我被困住了。一般来说,我不太擅长 mod_rewrite 或正则表达式,这给我带来了问题。

我需要重定向网址,例如
domain.com/view/Some_Article_Name.html

domain.com/index.php?p=view&id=Some_Article_Name

我现在的规则工作正常,但它也会重写我所有的样式表和图像以及不应该重写的内容。
RewriteRule ^view/([^/]*)\.html$ /index.php?p=view&id=$1 [L]

它应该只重定向以 domain 开头的页面。 com/view/*。我想我所需要的只是一个重写条件,但我似乎无法制作一个有效的。知道我需要添加什么才能使其正常工作,而无需为每个单独的文件编写重写规则吗?

RewriteCond %{REQUEST_FILENAME} ^(.*)\.html [NC]
RewriteRule ^view/([^/]*)\.html$ /index.php?p=view&id=$1 [L]

是我对此的重写声明。

So I'm stuck. I'm not very good with mod_rewrite or regular expressions in general, and this is giving me problems.

I need to redirect url's like
domain.com/view/Some_Article_Name.html
to
domain.com/index.php?p=view&id=Some_Article_Name

The rule that I have now works fine, but it also rewrites for all my stylesheets and images and stuff that shouldn't be rewritten.
RewriteRule ^view/([^/]*)\.html$ /index.php?p=view&id=$1 [L]

It should only redirect pages that start with domain.com/view/*.I imagine that all I need is a rewritecond, but I can't seem to make one that works. Got any idea what I need to add to this to make it work without writing a rewriterule for every individual file?

RewriteCond %{REQUEST_FILENAME} ^(.*)\.html [NC]
RewriteRule ^view/([^/]*)\.html$ /index.php?p=view&id=$1 [L]

is my rewrite statement for this.

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

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

发布评论

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

评论(2

不醒的梦 2024-09-09 02:09:09

我怀疑您的样式表和图像是否受到此规则的影响。因为这意味着您的样式表和图像的 URL 路径以 .html 结尾。因为否则该规则将不会被应用。

我宁愿假设您使用相对 URL 路径来引用样式表和图像,例如 ./css/style.cssimages/foo/bar.png。此类相对 URL 路径是从基本 URL 路径解析的。这就是当前文档的 URL 路径。

您的原始 URL 路径只有一个路径段,从该点开始,所有相对 URL 路径都有效。但现在您引入了另一个路径段 (/view),并且相对路径是从该路径解析的。因此 ./css/style.cssimages/foo/bar.png 现在解析为 /view/css/style.css 并且/view/images/foo/bar.png 而不是 /css/style.css/images/foo/bar.png 喜欢它与 /index.php 一起使用。

解决方案:使用绝对 URL 路径(如 /css/style.css/images/foo/bar.png)来引用外部资源。使用此类 URL 路径,基本 URL 可以具有任何路径,并且资源始终能够正确解析。

I doubt that your stylesheets and images are being affected by this rule. Because that would mean your stylesheets’ and images’ URL paths end with .html. Because otherwise the rule won’t be applied.

I rather suppose that you’re using relative URL paths to reference the stylesheets and images like ./css/style.css or images/foo/bar.png. Such relative URL paths are resolved from a base URL path. And that is the URL path of the current document.

Your original URL path had just one path segment and all relative URL paths worked when starting at that point. But now you introduces another path segment (/view) and relative paths are resolved from that path. So ./css/style.css or images/foo/bar.png is now resolved to /view/css/style.css and /view/images/foo/bar.png instead of /css/style.css and /images/foo/bar.png like it was with /index.php.

The solution: Use absolute URL paths like /css/style.css or /images/foo/bar.png to reference your external resources. With such URL paths the base URL can have any path and the resources are always getting resolved correctly.

挽清梦 2024-09-09 02:09:09

你是说你想要 css &图像位于原始文件位置下(例如,在 htdocs/view/ 上,其中 htdocs 是您的源根目录,但您希望将 html 的 url 重新路由到索引.php 文件?

如果这就是您所说的,那么您还需要一个反向 RewriteRule ,因为浏览器会选择与重定向 url 相关的任何 css/images(假设相对 image/ 。

除此之外,我同意@David,并且无法匹配任何没有 view.html 的内容

Are you saying that you want the css & images to live under the original file location (e.g. on htdocs/view/ where htdocs is your source root, but you want the url for the html to be rerouted to the index.php file?

If that is what you're saying, than what you need is a reverse RewriteRule as well, since the browser will pick any css/images relative to the redirected url (assuming relative image/css references in html).

Other than that, I agree with @David and can't how anything w/o view and .html could be matching.

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