简体   繁体   English

RewriteRule工作在.htaccess但不是httpd.conf

[英]RewriteRule working on .htaccess but not httpd.conf

I'm trying to redirect all urls that have /__/ to /public/assets/ and got it working in .htaccess but not globally in httpd.conf What it should do is re-write: 我正在尝试将所有具有/__/ URL重定向到/public/assets/并使其在.htaccess中工作但在httpd.conf中不是全局的。它应该做的是重写:

http://example.com/__/test.css

to

http://example.com/public/assets/test.css

So far I have this working for .htaccess: 到目前为止,我已经为.htaccess工作了:

RewriteRule ^(.*)/?__/(.*)$ $1/public/assets/$2 [L]

(Which might not be written properly but it's the only way I managed to make it work). (可能没有正确编写,但这是我设法使其工作的唯一方法)。 If I put it in httpd.conf however, it doesn't work at all. 如果我把它放在httpd.conf中,它根本不起作用。 I tried tweaking it, tried putting it inside <VirtualHost 127.0.0.1> and inside <Directory "c:/DEV/wamp/www/"> but it still won't work. 我试过调整它,尝试把它放在<VirtualHost 127.0.0.1><Directory "c:/DEV/wamp/www/">但它仍然无法正常工作。 Been losing 3 hours of time on this now and found plenty of examples on Stackoverflow/forums, but oddly none of them work for me.. 现在已经失去了3个小时的时间,并在Stackoverflow /论坛上找到了大量的例子,但奇怪的是它们都不适合我。

EDIT: Here are two examples of blocks I tried. 编辑:这是我尝试过的两个块的例子。 [R] is to verify if the rule was working and redirecting: [R]是验证规则是否正常工作并重定向:

<VirtualHost 127.0.0.1>
    DocumentRoot "c:/DEV/wamp/www/"
    <Directory "c:/DEV/wamp/www/">
        Allow From All
        RewriteEngine On
        RewriteRule ^/(.*)?__/(.*)$ /$1public/assets/$2 [R]
    </Directory>
</VirtualHost>

And this one: 还有这个:

<Directory "c:/DEV/wamp/www/">
    Allow From All
    RewriteEngine On
    RewriteRule ^/(.*)?__/(.*)$ /$1public/assets/$2 [R]
</Directory>

EDIT2: Tried selected edit from proposed answer, still cannot find the css files on the server. EDIT2:尝试从建议的答案中选择编辑,仍无法在服务器上找到css文件。

<Directory "c:/DEV/wamp/www/">
    Allow From All
    RewriteEngine On
    RewriteRule ^__/(.*)$ public/assets/$1 [L]
</Directory>

RewriteRule ^/(.*)?__/(.*)$

You've included a slash prefix on your RewriteRule pattern in the <Directory> container. 您已在<Directory>容器中的RewriteRule模式中包含斜杠前缀。 This should be the same as in your .htaccess file, ie. 这应该与.htaccess文件中的相同,即。 no directory-prefix. 没有目录前缀。

.htaccess files and <Directory> containers behave the same in this respect. .htaccess文件和<Directory>容器在这方面的行为相同。 Only when the directive is directly in the server config / VirtualHost container should the directory-prefix be included (but then it matches the URL-path, not the filesystem path, as it does in a .htaccess / Directory context). 只有当指令直接在服务器config / VirtualHost容器中时,才应包含目录前缀(但它与URL路径匹配,而不是文件系统路径,就像在.htaccess /目录上下文中那样)。

If you put it in a <Directory> container then there should be no need to "tweak it". 如果你把它放在<Directory>容器中,那么就不需要“调整它”了。

I'm trying to redirect 我正在尝试重定向

Strictly speaking, this is an internal rewrite (as you have in your .htaccess file), rather than an external redirect . 严格来说,这是一个内部重写 (就像你的.htaccess文件中一样),而不是外部重定向


/__/<file> to /public/assets/<file> /__/<file>/public/assets/<file>
RewriteRule ^(.*)/?__/(.*)$ $1/public/assets/$2 [L]

This rewrite can be simplified, providing /__/ always occurs at the start of the URL-path. 这种重写可以简化,提供/__/始终发生在URL路径的开头。 Try the following in either a .htaccess file or <Directory> container: 在.htaccess文件或<Directory>容器中尝试以下操作:

RewriteRule ^__/(.*)$ public/assets/$1 [L]

[EDIT] WARNING: mod_rewrite inheritance [编辑]警告:mod_rewrite继承

If you are switching between testing .htaccess and <Directory> containers in your server config and wondering why the directives in the <Directory> container aren't doing anything then it's possible that the mod_rewrite directives in .htaccess are completely overriding the directives in the <Directory> container. 如果要在服务器配置中测试.htaccess和<Directory>容器之间切换,并想知道为什么<Directory>容器中的指令没有做任何事情,那么.htaccess中的mod_rewrite指令可能完全覆盖了指令中的指令。 <Directory>容器。 This is the default behaviour. 这是默认行为。 .htaccess directives will take priority and parent directives are not inherited by default. .htaccess指令将优先使用,默认情况下不会继承父指令。

Generally, if you are using <Directory> containers in the server config then you don't need .htaccess, so the easiest option is to simply disable them in your <Directory> container with AllowOverride None . 通常,如果您在服务器配置中使用<Directory>容器,那么您不需要.htaccess,因此最简单的选择是使用AllowOverride None<Directory>容器中禁用它们。 So, the <Directory> container looks like: 因此, <Directory>容器看起来像:

<Directory "c:/DEV/wamp/www/">
Allow From All
AllowOverride None
RewriteEngine On
RewriteRule ^__/(.*)$ public/assets/$1 [L]
</Directory>

If you needed to have mod_rewrite directives in both .htaccess and a parent <Directory> container then you would need to explicitly enable mod_rewrite inheritance by placing the following directive in .htaccess: 如果你需要在.htaccess和父<Directory>容器中都有mod_rewrite指令,那么你需要通过在.htaccess中放置以下指令来显式启用mod_rewrite继承:

RewriteOptions Inherit

If you are on Apache 2.4 then you have more options. 如果您使用的是Apache 2.4,那么您有更多选择。

You can't simply disable the rewrite engine in .htaccess (ie. RewriteEngine Off ) since that will also prevent the parent directives from being processed. 您不能简单地禁用.htaccess中的重写引擎(即RewriteEngine Off ),因为这样也会阻止处理父指令。 If you simply remove the RewriteEngine directive from .htaccess then the RewriteEngine On directive in the <Directory> container will enable the rewrite engine but will result in the .htaccess directives overriding the parent again, so you are back to requiring the RewriteOptions Inherit directive. 如果你只是删除RewriteEngine从的.htaccess指令则RewriteEngine On指令在<Directory>容器将使重写引擎,但将导致在.htaccess指令再次重写父,所以你又回到了需要RewriteOptions Inherit指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM