Remove directory name from Kirby generated links


#1

Hello,
maybe the question was already ask but I didn’t find a topic for it.

I mixed my kirby installation with a static site that already exist before.
The Kirby installation is in a subfolder called cms.
When I link to a main page created with Kirby the url looks fine examplecom/mykirbypage.
On one page, part of Kirby child-pages are shown and liked to the relative pages with <?= $page->url() ?>.
In the generated link is alway the the cms directory name visible examplecom/cms/mykirbypage/kirbysubpage.
How can I remove the /cms/?
When I write the url manually in the address bar without the cms examplecom/mykirbypage/kirbysubpage the pages works fine too.

I hope you understand the question.

Greetings,
Sven


#2

Should be possible with a route: https://getkirby.com/docs/developer-guide/advanced/routing


#3

Works great.

Thank you.


#4

okay. It dosen’t work.
The routing works only from the Kirby-Base Folder and deeper but not on the parent folder.
I try to figure out another solution.


#5

You can try to do it with a rewrite rule in your .htaccess.

What do you mean with the parent folder? Is that your static HTML page? If so, then Kirby routing can’t possibly work there, but you would just have to put the correct URLs there, i.e. without the cms part in there?


#6

my site is in the parent-folder with an index.php file and some other pages.
Kirby is installed in a subfolder called: cms

I added the following lines to my .htaccess file:
RewriteEngine On
RewriteRule ^ cms/ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*) cms/$1

But when I’m trying to access a child-page of a Kirby generated parent-page I get the “cms” back in the URL.
The link to the child-pages is generated with <?= $page->url() ?>
All the parent pages generated by kirby I can access without the “cms” in the URL.


#7

How about adding a route for Kirby on top of the .htaccess rule in the parent folder?


#8

I think the problem is in the generated link from Kirby.
When I insert the url example.com/kirbyparentpage/child-page it works fine
but the link points to example.com/cms/kirbypage/child-page.
In some way I have to remove the ‘cms’ from the generated link.


#9

You could create a custom page method (myUrl()) that returns the desired url; downside is that you would have to replace all url() methods with this custom url. With a custom method in a Page Model you could overwrite the native URL method, but you would have to create the model for each page type.


#10

I will take a deeper look at first I didn’t understand how it works.

But thanks.


#11

I solved the problem with just changing the href from $page->url() to $page->id().
Now its works as I want it.

Thanks for your help.