Forum

Thread tagged as: Question

Friendly url for list and detail page slugs not working

OK, I have list and detail pages which are created inside perch from a master page. These are all top level pages and I would like the detail pages to be redirected from https://www.mysite.com/example-product.php?s=slug-here to https://www.mysite.com/slug-here.

Since the pages are created in perch, I ultimately need some kind of wildcard for the file nameand redirect anything with a slug. I am not having much luck at all though, even if I just test using an existing file name!

OK, so I imagine I will need someting like this to redirect all files with slugs:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-/]+)$ /(*.)?s=$1 [L] </IfModule>

But that does nowt. So I tried adding a file neame to see if the wildcard was my issue, but it isn't! I tried this (based on your suggestion for clean urls in the help pages) and it doesn't work either! What am I missing?

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-/]+)$ /example-product.php?s=$1 [L] </IfModule>

Any help would be appreciated.

The below worked, but I need to put in the last bit to redirect internally and for that I can't use a wildcard. Any ideas why this would work and the above doesn't? I don't know enough about redirects to figure it out!

Options +FollowSymLinks -MultiViews

Turn mod_rewrite on

RewriteEngine On RewriteBase /

external redirect from

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+example-product.php\?s=([^\s&]+) [NC] RewriteRule ^ /%1? [L,R=301]

internal forward from

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^/]+)/?$ /example-product?s=$1 [L,QSA]

Lisa Morena

Lisa Morena 1 points

  • 6 years ago

Oooooh, hold on. I was expecting it to redirect the slug to the friendly url, which it doesn't but if I type in https://mysite.com/slug-here then I see it has worked!

Hmm, OK now to figure out the wild card then change the links from the list page....

Not having much like with the wildcard thing, so if anyone can chuck me some ideas, that's be grand :)

Rachel Andrew

Rachel Andrew 394 points
Perch Support

There is a useful article here, written by Drew, about URL rewriting: https://24ways.org/2013/url-rewriting-for-the-fearful/

Thanks for the reply, I will take a look, but I have acually this minute just sussed it out!!

I am using:

Options +FollowSymLinks -MultiViews

Turn mod_rewrite on

RewriteEngine On RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/([a-zA-Z0-9-/]+)$ $1.php?s=$2 [L]

To redirect https://www.mysite.com/example-product.php?s=slug-here to https://www.mysite.com/example-product/slug-here

My problem now is how to I link to that url from the product-listing template? I have

<a href="<perch:content id="slug" type="slug" />">

but I need to include the page name without the .php - how do I do that in perch?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You should just be able to update the link in your template.

Done with this:

PerchSystem::set_var('var_url', perch_page_url(array( 'hide-extensions' => true ), true));

<a href="<perch:content id="var_url" type="hidden" />/<perch:content id="slug" type="slug" />">

Just so others know, I needed to add this to my htaccess as it had stopped my perch directory working:

RewriteRule ^(perch)($|/) - [L]