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.



Link to comment
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; 
Link to comment
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 ...


Link to comment
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
Link to comment
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
Link to comment
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

  • Create New...