Jump to content

Redirects in htaccess

Recommended Posts


I have a problem with the correct redirect-rules. My site-structure is:

- continent 1

--- land 1

----- project 11

----- project 12

--- land 2

----- project 21

- continent 2


- stuff

--- donation form

Now I want to use the same donation form in all projects. The relevant project should be preselected in a select field. For this I would like to create a rewrite rule in .htaccess. The url segments for the template are already approved. The url /continent1/land1/project11/donation should be to /stuff/donation/project11/land1/continent1. What is the correct rule? I've tried a concrete example in the redirect module, but I get only a blank page with no error message. Where is my mistake? Please help me.



Share this post

Link to post
Share on other sites

Hi enibas

I wouldn't setup the redirects in the htaccess.

Instead use $session->redirect()

You have url Segments enabled for both project template and donation form?

Then something like this should work for the redirect:

// In project template
if ($input->urlSegment1 == 'donation') {
  $donationPageUrl = $pages->get('/stuff/donation/')->url;
  $redirectUrl = $donationPageUrl . "{$page->name}/{$page->parent->name}/{$page->parent->parent->name}/";

But why you need the redirect? Can't you use a normal link to your donation page?

Also why do you reverse the path? Otherwise the redirectUrl is simpler to build:

// This should result in /stuff/donation/continent1/land1/project11/
$redirectUrl = rtrim($donationPageUrl, '/') . $page->url; 

Share this post

Link to post
Share on other sites

Hi Wanze,

thanks for the support.

I'm convert a website from a terribly cumbersome cms to my favorite cms ProcessWire;-) So I'd like to receive as many existing paths. Therefore, the redirects. The redirection works, but the URL in the browser changes.

Can I keep the url /continent1/land1/project11/donation as address, although I'm actually here /stuff/donation/project11/land1/continent1?

Sorry, this is probably quite simple ...


Share this post

Link to post
Share on other sites

Hello enibas!

I believe you can achieve this pretty easily by using url segments. First, make sure url segments are enabled in homepage template settings and then try something like this in your template file:


// first segment is set
if ($input->urlSegment1) {
	// third segment is set
	if ($input->urlSegment3) {
		// get page from actual path
		$p = $pages->get("/stuff/donation/$input->urlSegment3/$input->urlSegment2/$input->urlSegment1/");
		// if page exists, render it and exit - else throw 404
		if ($p->id) {
			echo $p->render();
		} else throw new Wire404Exception();
	// first segment set but no mathes, throw 404
	throw new Wire404Exception();

// begin normal homepage
  • Like 1

Share this post

Link to post
Share on other sites

For stuff like this, I would be more inclined to use an Apache rewrite rule, just because there would be less overhead and code to make it happen. Something like this might work in your .htaccess (somewhere after the "RewriteEngine On" line): 

RewriteRule ^(continent[0-9]+)/(land[0-9]+)/(project[0-9]+)/donation/?$ /stuff/donation/$3/$2/$1/ [R=301,L]
  • Like 1

Share this post

Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By ce90
      Hello everyone,
      hopefully this is the right place to ask and is not a duplicate question.
      I'm pretty new to processwire, so... if this question is kind of funny for some of you, you're welcome 😄 
      I have the following issue and can't find anything understandable about it. Maybe I'm searching the wrong way, but anyways.. here is my question:

      How is it possible to rewrite the URLs, that I'll get a *.php ending?
      https://www.mysite.de/urlsegment/ -> https://www.mysite.de/urlsegment.php
      https://www.mysite.de/urlsegment/urlsegment/ -> https://www.mysite.de/urlsegment/urlsegment.php
      Because I've read a lot about "Why do you wanna do this or have that?" – here my answer for that in advance:
      I've built a processwire installation inside or around an existing website. Therefore, we want to keep the existing *.php Google entries. Sure, we could redirect via 301 Redirect, but would prefer to keep the *.php ending.
      If you have further questions, please do not hesitate to ask.
      Thank you in advance for your help.

      Best regards
    • By chuguruk
      Hi there,
      user profile visible as:
      So far visible as:
      404 template:
      <?php $username=$input->get->user;?> <? $hello = $users->get($username); echo $hello->name; echo $hello->id;?> I have added this to .htacces
      RewriteRule ^[A-Za-z-]+/([A-Za-z0-9-]+)/?$ user.show?user=$1 [NC,L] But it doesn't work.
      It would be much better to not use 404 template.. Any idea how to get this working?
      I've added page and template "show" with the above code.
      I have added this code to header.inc
      <?/** * 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 == 'show') { $event->replace = true; $event->return = "/$page->name/"; } });?> Seems like I'm missing something as it doesn't work for me.
      And I can't access users path anyway as it's restricted, right?
    • By icreation
      I'd be grateful if anyone has a solution to the following issue.
      By default Processwire builds its urls like this:
      On the surface this seems excellent for SEO but has been causing us a few issues. Let me explain.
      In a responsive environment a drop-down menu like below must activate on touch/click. And, the top level is NOT a page. (Obvious, when you consider that you cannot 'click' to this link with your finger).
      See this example.
      Below you will see that ABOUT US  is a top level menu with children. Is this arrangement on a Bootstrap menu ABOUT US cannot be clicked as it could be on a tablet. Therefore, ABOUT US is not a page. 
      Let's take the first child as an example. WHO WE ARE, would have its url structure built by Processwire like this:
      2 problems here:
      If we create breadcrumb navigation then ABOUT US becomes a link to a page that should not exist. /about-us/ on its own creates issues with SEO. In a complex system trying to exclude these from searches and sitemaps is a a real issue. What would solve the problem is the ability to rewrite the url structure. It would be preferable to write the example url as
      Can this be done without having to change any core code?
  • Create New...