Apache mod_rewrite 仅当请求不以“/THEMES/”开头

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1181781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 17:57:58  来源:igfitidea点击:

Apache mod_rewrite only if request does not start with '/THEMES/'

regexapachemod-rewrite

提问by Kevin

I'm writing a CMS in PHP, and now I'm working at the themes feature. I have a .htaccess file:

我正在用 PHP 编写一个 CMS,现在我正在研究主题功能。我有一个 .htaccess 文件:

RewriteEngine ON
RewriteRule ^([a-zA-Z][a-zA-Z0-9]*)$ index.php?m=
RewriteRule ^([a-zA-Z][a-zA-Z0-9]*)/(.+)$ index.php?m=&p=

If I have a request to:

如果我有以下要求:

/page

it must load the view function of the class called page.

它必须加载名为 page 的类的视图函数。

If I have a request to:

如果我有以下要求:

/page/test

it must load the view function of the class called page, with the parameter 'test'. This all works,

它必须加载名为 page 的类的视图函数,并带有参数“test”。这一切都有效,

But I want it to apply the RewriteRules ONLYif the request does NOTstart with:

但我想它应用的RewriteRules ONLY如果请求中下手:

/THEMES/

So I can apply CSS styles etc...

所以我可以应用 CSS 样式等...

Can anyone help me? Thanks.

谁能帮我?谢谢。

采纳答案by Gumbo

You could use an additional rule to stop the rewriting process:

您可以使用附加规则来停止重写过程:

RewriteRule ^THEMES/ - [L]
RewriteRule ^([a-zA-Z][a-zA-Z0-9]*)$ index.php?m=
RewriteRule ^([a-zA-Z][a-zA-Z0-9]*)/(.+)$ index.php?m=&p=

回答by Kevin

Add this before your rewrite rules:

在重写规则之前添加:

RewriteCond %{REQUEST_URI} !^/THEMES/

回答by sydarex

Do something like:

做类似的事情:

RewriteRule ^THEMES - [L]

That means: if the request starts with THEMES, just serve it.

这意味着:如果请求以主题开头,只需提供它。

Another possible solution is:

另一种可能的解决方案是:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 

That means: do not rewrite if the request resolves to an existing file (first line) or directory (second line).

这意味着:如果请求解析为现有文件(第一行)或目录(第二行),则不要重写。

Maybe you should read the documentations, is really well written.

也许你应该阅读文档,写得非常好。

回答by Ahmet Kak?c?

If you want to include css/js files while using url-rewriting use that tag to specify the main url.

如果您想在使用 url 重写时包含 css/js 文件,请使用该标签来指定主 url。

< base href="http://www.your-web-site-adress.com" >

Then you may easily include your css/js files like that:

然后你可以轻松地包含你的 css/js 文件:

< script src="/blabla.js" >

it'll add base href as prefix.

它将添加 base href 作为前缀。