Apache, .htaccess or RewriteBase problems on macOS with absolute/relative links …

Dear folks,

unfortunately I’m having problems on my macOS with either built-in Apache, .htaccess or the RewriteBase line in there.

My kirby websites are located in ~/Sites/somesite.dev … mod_rewrite IS enabled and working.

My .htaccess file is located at ~/Sites/somesite.dev/.htaccess … there’s no ~/Sites/.htaccess

My RewriteBase in that file is set to RewriteBase /somesite.dev

And some things are working fine I guess. But, when using relative URLs, I’m getting into trouble. I don’t like relative URLs, I just tried it out because I thought, hey, with relative URLs my problems would be gone. But not.

So, back to using absolute URLs … it seems, the RewriteBase is just ignored and all absolute URLs point to the RewriteBase’s parent directory, so the DocumentRoot of Apache.

How could we fix this issue? In my opinion, the RewriteBase should take care of working absolute URLs also.

Or would we need to edit kirby’s RewriteRules for this? : o

Thank you in advance!

PS: So, it should play any role where the kirby website is stored, as long es you set RewriteBase correctly to the DocumentRoot. Or do I get this wrong?

Your setup seems to be right to me. Maybe the implementation of your links isn’t correct. If you use hardcoded links (don’t do it) like …

<a href="/some/url">Link</a>

RewriteBase will be ignored. But if you use a Kirby method like …

<a href="<php echo $page->url()">Link</a>

RewriteBase should work correctly. If you want to link to a special page you can use this syntax:

<a href="<php echo page('some/page/uri')->url()">Link</a>

Dear @flokosiol, thank you very much! I think I’m mainly talking about <link> tags in header and your solution maybe solves half of the problem. Maybe it’s a good idea to add <?php echo $url ?> to all link tags … though, then they would be external links but local ones …

The other half of the problem is, that I probably cannot use those php snippets in my .css files, right? At least I tried it and I failed.

For example I’m using @import to include some other css files inside of my main index.css. Also, e.g. in files like fonts.css, where you specify where your different font variants are stored and such, you need to use valid paths.

What do you mean with PHP snippets inside .css files? Are you trying to echo path information inside CSS files? This will not work, you can only use PHP code inside php files.

Exactly. So I need to get url(…) in .css files working. I still did not figure out why this problems exists, maybe I should try to talk to some apache folks in #httpd on IRC about this.

As I said, that is not possible. You either have to hardcode your URLs in your stylesheets, or you have to use inline styles.

PHP is a server side language, it does not work in the browser.

Inside .css files I only use relative paths. I don’t know why this doesn’t work for you?!

The easiest way to link to a css file in your <head> section, is to use the css() method.


1 Like

Unfortunately I couldn’t find any solution to this behavior and it seems I just have to change my development environment from directory-based—which did not work reliably with neither absolute nor relative links—to virtual host based.

I wanted explicitly NOT use virtual hosts, because you have to be able to configure apache for this and most people only may use .htaccess on their (shared) server.

For even latest macOS, there’s a simple and short tutorial for achieving dynamic vhosts with built-in apache. I hope it’s ok to link it here, there are so many not-anymore-working or just super weird tutorials, so I was happy to find this one and I think it’s very intuitive.

Though talking to the #httpd experts, I’m not 100% sure if RewriteBase should work as I thought and still think it shall work. I’ll keep you updated when I found out. : )

PS: But unfortunately that still didn’t fix the problems I’m experiencing with accessing the panel. It works perfectly on my webserver, but when I’m mirroring everything to my localhost (with same permissions), then it doesn’t. Weird.

I must admit that I don’t have any experience with using the inbuilt Apache, since I’m happily using MAMP. But I would expect this to work similarly and I don’t have to set the RewriteBase on localhost at all with or without virtual hosts.