Needs work
Project:
Drupal core
Version:
main
Component:
other
Priority:
Normal
Category:
Feature request
Assigned:
Unassigned
Reporter:
Created:
1 Aug 2012 at 13:09 UTC
Updated:
2 Feb 2023 at 10:11 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #1
kristofferwiklund commentedI think it makes sense but I would recommend to change in the comment. Saying "Use custom rewrite rules defined elsewhere." might freak people about security issue but what it actually does is :
"This forces the current configuration to inherit the configuration of the parent. In per-virtual-server context, this means that the maps, conditions and rules of the main server are inherited. In per-directory context this means that conditions and rules of the parent directory's .htaccess configuration are inherited." Apache docs
So a comment like. "Use custom rewrite rules defined in vhost settings. Those rules are applied after rules defined in this file."
Comment #2
chx commentedThe handbook suggests http://drupal.org/node/138889 putting Inherit in your vhost config directly. It seems there's nothing to do here.
Comment #3
tobiassjosten commentedPutting
RewriteOptions Inheritin the vhost configuration does nothing to help this problem. All that achieves is carrying over configuration from the main server, into the vhost scope. The.htaccesswill still take precedence and disregard everything else in the end.I have tried to summarize the problem and rewritten the comment as per Kristoffer's suggestion. Please have another look and consider this?
Comment #4
chx commentedInstead of a blog post you need to write an issue summary. The documentation on this option is entirely unclear:
I simply supposed that slapping
RewriteOptions Inheritinto your vhost configuration applies to the whole vhost. Hardly an unreasonable thing to suppose... What you are saying is that this is not so? Then say just that and be done. We have a core issue queue for these things. Slamming me for this on twitter and a blog post is absolutely the wrong thing to do.Comment #5
tobiassjosten commentedAgain, I'm sorry if I came across like calling you out; that was never my intention. Glad we could resolve it on IRC though.
As for the issue, the problem is with how inheritance works. On the bottom level you have the main Apache server. Then there's the virtual host, where the Inherit option brings in configuration from the main server. Finally on the top level is the .htaccess and its Inherit option makes it re-use configuration from the virtual host.
When the .htaccess is found and used (with
AllowOverride Allin the<Directory>container) Apache will completely disregard any rewrite rules in the vhost or main server, if the file does not explicitly allow inheritance.This patch adds the
RewriteOption Inheritconfiguration to the .htaccess, which will allow site owners to add custom rewrite rules in their virtual host, without touching the .htaccess. All while keeping the original functionality of the .htaccess.There is one caveat however. Inherited rewrite rules are evaluated in the backwards order, starting with the ones in .htaccess. Because it uses a catch-all pattern, even with inheritance it will not allow arbitrary rewrite paths in the vhost.
For use cases like routing /robots.txt through Drupal instead of serving the file, this is perfectly fine. My simple patch fixes this and for that reason I think it's still valid.
All the other use cases could be fixed too but it'd require Apache >= 2.3.10, where
InheritBeforewas introduced. This makes vhost rewrite rules be evaluated first. I could add that to the patch, so that if you want this functionality you could just use a newer version of Apache and it would just work. Do you think that would be a good idea?Comment #6
chx commentedMaybe commented out? Because if you add an option that is not recognized by Apache it freaks out.
Comment #7
tobiassjosten commentedThe problem with that is then we're back to square one — you would have to go in and make changes to the `.htaccess` core file.
But you are of course right, we can't have Apache freak out.
One alternative is to use mod_version and apply this configuration only where supported. It would require enabling that module of course but this feature is entirely directed to advanced users anyway, so I think it's a fair tradeoff. Wouldn't you agree?
Adding a new patch for this.
Comment #8
ericthelast commentedI wholeheartedly second this patch. Currently, we have a use case that requires an additional rewrite rule to be placed in .htaccess for it to work. This is obviously a terrible idea as 1) it's hacking core, 2) every time core is updated we have to remember to re-apply the rules.
I could settle for using this patch but again...ever core update I'd have to remember to re-apply. Any ideas???
Comment #9
Letharion commentedFrom http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriteoptions:
Inherit
InheritBefore
If we add InheritBefore, one can make additions to the rules in the .htaccess file with your vhost file.
If we add Inherit, one can make additions and changes to the rules in the .htaccess file with your vhost file. (As the vhost file rules are now applied after .htaccess, one can override what the .htaccess file says)
I would argue that Inherit is a better option. Among the limited advanced users that will have a vhost file, what reason is there to limit their ability to potentially overwrite changes in the .htaccess file?
While I agree that it's a useful feature, I believe this is the wrong approach to take to solve the type of issues in #8. There is a far more generic solution to that in a make-file for example.
I can't see how this is a bug though, so I'm setting to feature request.
Comment #10
tobiassjosten commentedThanks for taking the time to review this!
The problem is with Drupal's finishing catch-all rewrite rule in the
.htaccess. If you useInheritwith that, any rewrite rules in your vhost will be moot, since everything will have been routed through the front controller already. I just tested with the following.It will show
index.htmlwhen I visit/asdf. Unfortunately I don't have access to Apache >2.3.10 (withInheritBefore) right now but I am positive it worked as expected last I experimented, while writing this patch.Comment #11
Letharion commentedI haven't done Apache inherit rules myself. I thought about it in terms of programming inheritance, but actually, "inherit" doesn't inherit, it prepends (inherit) or appends (inheritbefore) rules, which is quite different.
My comments in #9 with regard to Inherit vs InheritBefore should be ignored. Patch in #7 looks good.
Is this something we can write tests for? Do we have the capability with the test bots to setup different vhost files?
Comment #12
ericthelast commentedJust as an aside, using Drupal 7 I was able to use hook_url_inbound_alter() and preg_match() to simulate a rewrite rule. Not as optimal as a true rewrite but it did satisfy my immediate need.
Comment #19
daften commentedIs it still an option to do this? With composer being used, I suspect it might for some use cases be needed more now than before.
Comment #20
jelle_sJust to confirm: Patch from #7 still applies to 8.5.x-dev.
Comment #21
daften commentedThe patch still applies
Comment #22
alexpottThe .htaccess file is a file where we expect user modifications. I think the correct thing here is to add a commented out section like
I run my dev set up in a virtual document root which means I have to change .htaccess and have to manage incorporating any changes to .htaccess that come from HEAD.
Also, could suddenly enabling this option on existing sites have unexpected repercussions?
Comment #23
daften commentedChanges to .htaccess can be annoying and cumbersome. E.g. we have a build server that assembles our code, so we have to just use the .htaccess from drupal core as is. Same for a lot of people that use shared hosting.
This change has NO impact for existing users, none whatsoever. Unless you've already put rewrite rules in a htaccess file higher up, which would be a very strange scenario ... :)
This allows you to put rewrite rules in another .htaccess file in a folder higher in the folder hierarchy of the server, which is otherwise impossible. ALL other htaccess items can already be put in such a htaccess file and just work. Also, the rewrite rules in the "other" htaccess file are applied before the rules in the drupal htaccess file, so the drupal access rules are still the "master".
Comment #24
alexpottWhen you are dealing thousands or sites there is pretty much a case of every strange scenario. As pointed out in #22 .htaccess is meant to be customised and there is no way we can provide something that just works for everyone. But we should not change it to something that might break an existing site. For me we should improve the documentation in the .htaccess file and move on. If we want to do this otherwise we need to wait till Drupal 9 as there is no need to make a potential breaking change till then.
If you have a build server it could easily assemble your htaccess file for you.
Comment #25
daften commentedI don't agree at all that this would break anything. And assembling a htaccess file on a build server is not something that can be done extremely easily. But fine, I haven't seen every user's setup.
One question though: how can we make sure this issue will actually be picked up for drupal 9 and not lie around for another 6 years. It'd be a shame to let a change like this lying around because it doesn't get picked up. And do we need additional documentation then on drupal.org in the handbook for this?
And thanks for responding. If i seem pissed off, it most likely is because I am, because we struggled with this for quite a while, and this would have been so helpful.
Comment #33
catchThe way to move this issue forward would have been to implement the feedback from #22. That could still happen so marking needs work for that.
Composer scaffolding makes it a lot easier to manage .htaccess in git.
https://www.drupal.org/docs/develop/using-composer/using-drupals-compose...