Mod_rewrite is not including subdirectories

丶灬走出姿态 提交于 2019-12-24 21:19:23

问题


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.

What I'm trying to accomplish is this:

  • ONLY rewrite files in the pages folder, but include ALL subdirectores of it.

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

Here's the code:

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]

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

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


回答1:


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:

From:

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

to:

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

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

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

<base href="/">


来源:https://stackoverflow.com/questions/15929517/mod-rewrite-is-not-including-subdirectories

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!