pwFoo Posted January 31, 2014 Share Posted January 31, 2014 Hello, I've just had little time... so I write a short post and come back later I thinking about organize my (visible) pages for example at "/content/*". So I'll get links like "/content/about" or "/content/myPage". Is it possible to create a path alias like "/about" or "/myPage" without the parent "/content/"? Finally I try to get a tree like this. [home] /content/ /tags/ /categories/ /settings/ 1 Link to comment Share on other sites More sharing options...
adrian Posted January 31, 2014 Share Posted January 31, 2014 There are a few different ways to do this, including htaccess rewrites, but this code from willyc is a good option: http://processwire.com/talk/topic/1799-routes-and-rewriting-urls/ A few other posts worth reading: http://processwire.com/talk/topic/4847-redirect-in-htaccess/ http://processwire.com/talk/topic/3275-hide-parent-page-from-url/ http://processwire.com/talk/topic/4521-how-to-customize-urls/ 5 Link to comment Share on other sites More sharing options...
pwFoo Posted February 1, 2014 Author Share Posted February 1, 2014 Hi Adrian, thanks for posting a few different waysd to do it htaccess / rewrite will work, but isn't a solution for me (dependent from webserver / server config). Topic Routes and rewriting URLS could be a solution or I rethink my website structure. Link to comment Share on other sites More sharing options...
Macrura Posted February 1, 2014 Share Posted February 1, 2014 i do this sort of thing now a lot, thanks to Ryan's CMS Critic Case Study; this is almost the same as WillyC's code at the above link /** * This hook modifies the default behavior of the Page::path function (and thereby Page::url) * * The primary purpose is to redefine blog posts to be accessed at a URL off the root level * rather than under /posts/ (where they actually live). * */ wire()->addHookBefore('Page::path', function($event) { $page = $event->object; if($page->template == 'post') { // ensure that pages with template 'post' live off the root rather than '/posts/' $event->replace = true; $event->return = "/$page->name/"; } }); 9 Link to comment Share on other sites More sharing options...
OllieMackJames Posted June 7, 2014 Share Posted June 7, 2014 i do this sort of thing now a lot, thanks to Ryan's CMS Critic Case Study; this is almost the same as WillyC's code at the above link /** * This hook modifies the default behavior of the Page::path function (and thereby Page::url) * * The primary purpose is to redefine blog posts to be accessed at a URL off the root level * rather than under /posts/ (where they actually live). * */ wire()->addHookBefore('Page::path', function($event) { $page = $event->object; if($page->template == 'post') { // ensure that pages with template 'post' live off the root rather than '/posts/' $event->replace = true; $event->return = "/$page->name/"; } }); Great stuff, one question, where would I place this to make it work? thanks! Link to comment Share on other sites More sharing options...
SiNNuT Posted June 7, 2014 Share Posted June 7, 2014 A convenient way would be to put the code in a file called _init.php. Create this file in your template directory and edit site/config.php to enable _init.php. The lines in config.php you need to look for are about 'prependTemplateFile', around line 45, read simple instruction. For some more ideas and insights you can, as Macrura mentioned, look at https://processwire.com/talk/topic/3987-cmscritic-development-case-study/ 2 Link to comment Share on other sites More sharing options...
OllieMackJames Posted April 27, 2015 Share Posted April 27, 2015 Finally getting around to working on this. But running into trouble. I have a site that is very important business wise for me, (it does very well in the search engines) and it uses .html extension for all pages directly off root It now still runs on modx evolution but now is the time to port to pw. I am not ready to try out redirects because no one will guarantee that this will give me the same serp results as I am getting now, so that is not an option. I have got one part working on a test site, which is getting the url to link to show each article living directly off root, I used this code in _init.php: wire()->addHookBefore('Page::path', function($event) { $page = $event->object; if($page->template == 'article') { // ensure that pages with template 'article' live off the root rather than '/posts/' $event->replace = true; $event->return = "/$page->name.html"; } }); So far so good: when I go to the home page (and any other page: it shows all links to articles as living off root with an .html extension. But this is where it stops working. I set allow urlsegmetns to yes on home.php template and added the following to home.php template: if(strlen($input->urlSegment2)) { // we only accept 1 URL segment here, so 404 if there are any more throw new Wire404Exception(); } else if(strlen($input->urlSegment1)) { // render the blog post named in urlSegment1 $name = $sanitizer->pageName($input->urlSegment1); $post = $pages->get("/tuning-tips/")->child("name=$name"); if($post->id) echo $post->render(); else throw new Wire404Exception(); } But I keep getting 404 errors, it does not show the page. when going to the /directly-off-root.html url. My setup on the site is like follows: home =>/category1toplevelpage/ => =>/category1toplevelpage/article1 => =>/category1toplevelpage/article2 => =>/category1toplevelpage/article3 => =>/category1toplevelpage/articleetcetc =>/category2toplevelpage/ => =>/category2toplevelpage/article1 => =>/category2toplevelpage/article2 => =>/category2toplevelpage/article3 => =>/category2toplevelpage/articleetcetc What code should I put where to get this to work properly? Help much appreciated! Thanks. Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 27, 2015 Share Posted April 27, 2015 Your selector is wrong "name=$name" would be parsed to "name=directly-off-root.html", but your page's name is still only "directly-off-root" without the html. As you're not using not html'ed names I would suggest just adding it to the name of the pages directly. Your hook to Page::path just changes to url on runtime, but does not modify the "name" field your selector checks for. To keep your code clean it's also best to not couple two different things, appending ".html" and masking out a parent page, into a single hook. Then you'll see that Page::path isn't the right place to handle your first issue. Link to comment Share on other sites More sharing options...
OllieMackJames Posted April 27, 2015 Share Posted April 27, 2015 @LostKobrakai, thanks, I tried it all without the .html extension and still no luck, any ideas how to get this working? I am not a coder, I can only copy paste, that is probably why I do not get this going yet. Anybody willing to get access to the test site to get it working, I will then do a proper write up for other dummies like me who might have use for it. Thanks! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now