Jump to content

Need a bit of help with .htaccess redirects in PW


John W.
 Share

Recommended Posts

Hi,

I'm on my last step before going live with a site, and I'm having an issue with redirects in PW.

on my old processwire site, for example, I have a news article on my old site named "rabbit-workshop", just a news article about an upcoming rabbit raising class in the area.

/news/rabbit-workshop/

what I need to do on my new site is add an additional path:

/news/county-name/rabbit-workshop

I'm not sure where to add this in the PW .htaccess file.

Also, my redirect isn't working properly. Any help would be appreciated.

RewriteEngine On
RewriteRule ^news/(.+)$ /news/washington-county-news/$1 [L,R=301]
 
Link to comment
Share on other sites

1 hour ago, John W. said:

on my old processwire site, for example, I have a news article on my old site named "rabbit-workshop"

As a side note, if the new site is an enhancement of the old site (rather than a totally new site built from scratch) and you have PagePathHistory installed (a must-have on every site) then you should be able to move the existing news articles to any new path and PW will automatically redirect visitors from the old location to the new location.

But if it's a new site from scratch where the IDs of historical news articles are now different to the old site, then I suggest doing the redirects using the PW API rather than manually entering a long list of redirects in htaccess.

There are different ways you could do it. You could hook ProcessPageView::pageNotFound or use URL segments on the template for the /news/ page. Both would be fine, but I think the latter is a bit easier.

Enable URL segments on the template for the /news/ page, and at the top of the template file:

// If there is a URL segment (i.e. no real page exists at the requested URL)
if($input->urlSegment1) {
	// Check for a published news article with that name
	$article = $pages->findOne("template=your_news_article_template, name=$input->urlSegment1");
	if($article->id) {
		// If match found then redirect
		$session->redirect($article->url);
	} else {
		// Otherwise throw 404
		throw new Wire404Exception();
	}
}
// Only one segment is allowed
if($input->urlSegment2) throw new Wire404Exception();

 

  • Like 3
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...