我有一个mod_rewrite(主要归功于堆栈成员 Olaf(重定向自 index.php?URL=pages/the-page.php
简单地the-page
.问题是,当请求一个页面不直接在pages
(即pages/sub
(时,它会重定向我的.css并.js这不好。
我想要完成的是:
- 仅重写
pages
文件夹中的文件,但包括它的所有子控制器。
重要的是,它永远不会重写files
文件夹的URL。
代码如下:
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 错误。
它还会杀死外部链接,使其.com/www而不是适当的地址,这是一个问题。
你不应该弄乱CSS和脚本的mod_rewrite规则。这听起来像是一个相对 URI 问题。将链接更改为绝对 URI:
从:
<link rel="stylesheet" href="style.css" type="text/css">
自:
<link rel="stylesheet" href="/style.css" type="text/css">
或者无论"风格.css"的绝对路径是什么。
如果您不想更改所有链接,请在页面标题中添加 URI 基
:<base href="/">