Mod_rewrite 不包括子目录 [英] Mod_rewrite is not including subdirectories

查看:58
本文介绍了Mod_rewrite 不包括子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 mod_rewrite(主要归功于堆栈成员 Olaf),它从index.php?URL=pages/the-page.php 简单地the-page.问题是,当请求的页面不是直接在 pages(即 pages/sub)中时,它会重定向我的 .css 和 .js,这是不好的.

I have a mod_rewrite (mostly thanks to stack member Olaf) that redirects from index.php?URL=pages/the-page.php to simply the-page. The problem is, when a page is requested that isn't directly in pages (i.e., pages/sub), it redirects my .css and .js which is no good.

我想要完成的是:

  • 仅重写 pages 文件夹中的文件,但包括它的所有子目录.
  • ONLY rewrite files in the pages folder, but include ALL subdirectores of it.

重要的是,它永远不会重写 files 文件夹的 URL.

An important thing is that it never, ever, ever rewrites the URL of the files folder.

代码如下:

Options +FollowSymlinks
RewriteEngine on
# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

# redirect the client
RewriteCond %{QUERY_STRING} URL=pages/(.+?)\.php
RewriteRule ^index\.php$ /gr/%1? [R,L]

# exclude rewriting all files located in /gr/files
RewriteCond %{REQUEST_URI} !^/gr/files/
# rewrite to real content
RewriteRule ^.*$ /gr/index.php?URL=pages/$0.php [L]

我花了无数个小时来处理这段代码.即使是很小的更改也会引发 404 或 500 错误.

I've spent countless hours fighting with this code. Even minor changes throws it to a 404 or 500 error.

它还会杀死外部链接使其成为 .com/www 而不是适当的地址,这是一个问题.

It also kills external links making it .com/www instead of the appropriate address, which is a problem.

推荐答案

您不应该弄乱 CSS 和脚本的 mod_rewrite 规则.这听起来像是一个相对 URI 问题.要么将您的链接更改为绝对 URI:

You shouldn't mess with the mod_rewrite rules for the CSS and scripts. This sounds like a relative URI issue. Either change your links to be absolute URI's:

来自:

<link rel="stylesheet" href="style.css" type="text/css">

到:

<link rel="stylesheet" href="/style.css" type="text/css">

或者style.css"的绝对路径是什么.

or whatever the absolute path to "style.css" is.

如果您不想更改所有链接,请在页面标题中添加一个 URI 基础:

If you don't want to change all of your links, then add a URI base to the header of your pages:

<base href="/">

这篇关于Mod_rewrite 不包括子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆