Forum

Thread tagged as: Problem, Runway

Perch Runway and URL Rewriting

Hi guys,

I'm trying to rewrite some URLs via .htaccess, and although the rule I've written should be working and applying as expected, it's not.

Below is my .htaccess:

RewriteEngine On

# HTB - /shop/ to /product (and other functionality)
RewriteRule ^shop/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ product?t=$1&h=$2 [NC]

# Perch Runway
RewriteCond %{REQUEST_URI} !^/cms 
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /cms/core/runway/start.php [L]

Is it possible that the Perch Runway block of code is causing the problem here? At present, the URL doesn't seem to redirect at all and just 404's, even though the testing tool here (https://htaccess.mwl.be/) confirms that the rule is met.

Any help is appreciated! Harry

Harry Ray

Harry Ray 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Would you not just use the built in routing for this?

Is the built in routing able to cope with this? Reading through the docs (and the blog post), I couldn't really figure out whether it could cope with this.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, something like:

shop/[slug:t]/[slug:h]
Drew McLellan

Drew McLellan 2638 points
Perch Support

Ah, sorry, are you actually looking to issue an HTTP redirect, rather than rewriting?

Yep - I'm looking to redirect URLs like "/shop/hats/trucker-hat" to "/product?t=hats&h=trucker-hat" - sorry for the confusion!

Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you probably need to stop processing and redirect. So [NC] might be [NC,R,L]

Hi Drew,

Unfortunately, I can't use the R flag as it changes the URL in the browser, which is why I was looking for a rewrite (and redirect) rather than just a redirect.

Is this achievable in Runway?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I'm not sure I know what that means.

No worries.

When a visitor reaches the site, and clicks on a product (let's say the "Trucker Hat" product), I would like to use an SEO-friendly URL like /shop/hats/trucker-hat.

However, in the back end, that's not a real file path - the actual page is product.php.

product.php contains code to break up a query string and fetch the appropriate product. It expects a URL like this: /product?t=hats&h=trucker-hat.

The .htaccess rule I've written for this is:

RewriteRule ^shop/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ product?t=$1&h=$2 [NC]

This would be ideal, as it would maintain the friendly URL in the browser but actually send the visitor to the unfriendly URL, because it's a RewriteRule - am I correct in saying that that's how Rewrite works in .htaccess?

Either way, I've used this testing tool (https://htaccess.mwl.be/) and confirmed that the rule I've written works as expected.

However, when I put the rule into my htaccess file, which also contains the Perch Runway htaccess code,(below):

# Perch Runway
RewriteCond %{REQUEST_URI} !^/cms 
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /cms/core/runway/start.php [L]

it doesn't work.

Here is my full htaccess file:

RewriteEngine On

# HTB - /shop/ to /product
RewriteRule ^shop/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ product?t=$1&h=$2 [NC]

# Perch Runway
RewriteCond %{REQUEST_URI} !^/cms 
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /cms/core/runway/start.php [L]

I tried using your suggestion of:

RewriteRule ^shop/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ product?t=$1&h=$2 [NC,R,L]

but that caused the URL to change (and actually still didn't work, taking me to a file path from the root of the server, but I'm sure it would have worked after some tweaking). We need the URL to not change from the friendly URL.

Did that clarify things?

Is this possible in Perch Runway?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok, that's basic URL rewriting, which Runway's routing will handle for you. You don't need any redirects.

Awesome!

Are you able to provide an example of how I'd get /shop/hats/trucker-hat to point to /product?t=hats&h=trucker-hat?

I'm struggling to get to grips with it just from the docs, I'm afraid!

Duncan Revell

Duncan Revell 78 points
Registered Developer

On the location tab of your product.php page, add this as a URL pattern:

Drew McLellan said:

Yes, something like:

shop/[slug:t]/[slug:h]

In the product.php code, you can use perch_get('t') as you would before.

No need for any extra rewriterules.