Forum

Thread tagged as: Question, Configuration, Runway

Runway and .htaccess URL rewriting guidance

Howdy,

I have the following URLs as a result of list and detail (with optional category):

LISTINGS
/projects/
/projects/?cat=printdesign

DETAIL
/projects/?s=microsoft
/projects/?cat=printdesign&s=microsoft

My goal is to rewrite as follows:

LISTINGS
/projects/
/projects/printdesign/

DETAIL
/projects/microsoft/
/projects/printdesign/microsoft/

I am an .htaccess rewrite dunce so I have two main questions which hopefully some folks could guide me along with:

1) Before I go diving into custom rewrite rules, should I instead be leaning on Runway's 'routes'? If not, am I okay to add custom rules or is this not recommended as it could cause mayhem with Runway's routing?

2) As the category part of the path is optional, I am guessing that it would be hard to make a rewrite rule for /projects/microsoft/ as it will assume 'microsoft' is a category? If so, perhaps when no specific category is needed, it should be /projects/all/microsoft/?

Any help/guidance/RTFM welcomed.

Churz, Keir

Keir Moffatt

Keir Moffatt 0 points

  • 5 years ago

Keir Moffatt said:

Howdy,

I have the following URLs as a result of list and detail (with optional category):

LISTINGS
/projects/
/projects/?cat=printdesign

DETAIL
/projects/?s=microsoft
/projects/?cat=printdesign&s=microsoft

My goal is to rewrite as follows:

LISTINGS
/projects/
/projects/printdesign/

DETAIL
/projects/microsoft/
/projects/printdesign/microsoft/

Use Runway Routes

/products/[*:cat]
/products/[*:cat]/[slug]

Thanks @Robert, appreciated - your post and https://docs.grabaperch.com/runway/routing/ helped me along.

I have managed to get the categories working, but not the slug. Also, if I provide just /projects/project-slug/ it, as I feared, assumes project-slug is a category.

I also tried removing the category part of the routing, so just /products/[slug] but no dice either.

Any further thoughts?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I guess the issue is that there's no difference between

/projects/printdesign/

and

/projects/microsoft/

as far as any dumb pattern matching the URL is concerned.

If you need to have that URL format you'll have to route them to the same page (as they will anyway) and query the CMS to find what sort of mode you should be in.

Yeah, I suspected such. I may simply use /all/ when no category is present.

That aside, any ideas why I am struggling to get just slug rewriting working?

E.g. /projects/?s=microsoft to /projects/microsoft

My route in page options is projects/[slug] and I am using the following list & detail code:

if (perch_get('s')) { // output project detail

  perch_content_custom('Projects', array(
    'template' => 'project.html',
    'filter' => 'slug',
    'match' => 'eq',
    'value' => perch_get('s'),
    'count' => 1,
  );

} else { // output project listing

  perch_content_custom('Projects', array(
    'template' => 'project_listing.html'
  );

}

It I go to /projects/microsoft, it is outputting the listing page so I suspect the slug part of the URL is not being rewritten to ?s=

Thanks folks

try:

projects/[slug:s]

Ding ding ding! Thank you, that worked.

I will now go and play to see how to get categories involved either using /all/ or some internal logic.

Thanks both, very much appreciated - will post my final outcome.

Using Route: projects/[slug:s] ...

get value of ('s')

perch_get('s'); // value of slug

Without the :s (name) you could get the value as...

perch_get(1); // this would give you slug
perch_get(0); // this would give you entire query

Thanks Robert - I will give that a go next.

In the meantime, I did manage to get it working by forcing /all as the default category:

// always make any category slug available to templates (default to all if blank)
$category = (!perch_get('cat')) ? 'all' : perch_get('cat');
PerchSystem::set_var('current_category', $category);

...and...

// if we have a category... extend options to limit results to specified category
if (perch_get('cat') && perch_get('cat') != 'all') {
  $opts['category'] = 'project-types/' . perch_get('cat');
}

Will report back if I succeed with your suggestion.

Dang, really useful to be able to use perch_get([querystring_array]) but would still either need some logic to check different between /projects/category-slug/ and /projects/project-slug/ OR would need me to split the list and detail over two pages, e.g. /projects/ and /projects-detail/

I reckon I'll stick with the all/ solution for now and try this approach next time.

Thank you so much for your help!