Welcome to WebmasterWorld Guest from 35.172.195.49

Forum Moderators: Ocean10000 & phranque

Rewrite, Canonical or Both?

     
11:09 am on Mar 29, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 17, 2002
posts: 1189
votes: 6


I have a site that allows searching for persons names in different categories. The resulting url will be of this format:

example.com/location-game-person

e.g.

example.com/north-baccarat-john-steed

The script that is used to search for and display data is /php/namesearch.php and I have a few rewrite rules in httpd.conf to process this:

RewriteRule ^north-baccarat-(.*)$ /php/namesearch.php?location=north&game=baccarat&person=$1 [L]
RewriteRule ^south-baccarat-(.*)$ /php/namesearch.php?location=south&game=baccarat&person=$1 [L]
RewriteRule ^north-bridge-(.*)$ /php/namesearch.php?location=north&game=bridge&person=$1 [L]

That works fine but I would like to change the first two dashes to forward slashes:

example.com/north/baccarat/john-steed

To do that I just added this:

RewriteRule ^north/baccarat/(.*)$ /php/namesearch.php?location=north&game=baccarat&person=$1 [L]
RewriteRule ^south/baccarat/(.*)$ /php/namesearch.php?location=south&game=baccarat&person=$1 [L]
RewriteRule ^north/bridge/(.*)$ /php/namesearch.php?location=north&game=bridge&person=$1 [L]

That works well too, but the search engines still have the old location-game-person and I think I will be getting duplicates for this.

So should I now have a rewrite for the dashes to slashes?

example.com/location-game-person

to

example.com/location/game/person

Or should I just use a canonical? Or should I do both?
11:27 am on Mar 29, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11871
votes: 245


So should I now have a rewrite for the dashes to slashes?

example.com/location-game-person

to

example.com/location/game/person

you want an external redirect with a 301 status code.
6:29 pm on Mar 29, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15934
votes: 889


To do that I just added this:
But who will request the forms with / if there's no redirect in place?

And, psst!
RewriteRule ^(north|south)-(\w+)-(.+) http://www.example.com/$1/$2/$3 [R=301,L]
or
(baccarat|bridge|whist|piquet)
if there's just a small number of options for the second bit.

I’m leery of the final .* because that seems like a malformed URL if there’s no content after the last - or / (whichever it may be). You don’t actually need a $ because Regular Expressions will continue to the end by default unless you expressly tell them to stop sooner.
9:32 pm on Mar 29, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 17, 2002
posts: 1189
votes: 6


Ok, with Phranque's suggestion I now have:


RewriteRule ^north/baccarat/(.*)$ https://www.example.com/north/baccarat/$1 [L]
RewriteRule ^south/baccarat/(.*)$ https://www.example.com/south/baccarat/$1 [L]
RewriteRule ^north/bridge/(.*)$ https://www.example.com/north/bridge/$1 [L]

RewriteRule ^north/baccarat/(.*)$ /php/namesearch.php?location=north&game=baccarat&person=$1 [L]
RewriteRule ^south/baccarat/(.*)$ /php/namesearch.php?location=south&game=baccarat&person=$1 [L]
RewriteRule ^north/bridge/(.*)$ /php/namesearch.php?location=north&game=bridge&person=$1 [L]


That is working as required.

But who will request the forms with / if there's no redirect in place?


I don't quite get that.

The form search page is

example.com/north/baccarat/

or

example.com/south/bridge/

and the other combinations depending on what the user is searching for. Either of those page will explain the page and offer a search box. Searching for and finding a successful match will then load a page like this:

example.com/south/bridge/john-steed

and give info for the person and his bridge performance in the south.

RewriteRule ^(north|south)-(\w+)-(.+) http://www.example.com/$1/$2/$3 [R=301,L]


ahh, individual rules can be combined with ors and ands? I like that for coolness but is there any performance hit with this?
10:21 pm on Mar 29, 2019 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:11871
votes: 245


you want an external redirect with a 301 status code.

Ok, with Phranque's suggestion I now have:


RewriteRule ^north/baccarat/(.*)$ https://www.example.com/north/baccarat/$1 [L]
RewriteRule ^south/baccarat/(.*)$ https://www.example.com/south/baccarat/$1 [L]
RewriteRule ^north/bridge/(.*)$ https://www.example.com/north/bridge/$1 [L]

RewriteRule ^north/baccarat/(.*)$ /php/namesearch.php?location=north&game=baccarat&person=$1 [L]
RewriteRule ^south/baccarat/(.*)$ /php/namesearch.php?location=south&game=baccarat&person=$1 [L]
RewriteRule ^north/bridge/(.*)$ /php/namesearch.php?location=north&game=bridge&person=$1 [L]


That is working as required.

without specifying the [R=301] flag those redirects will default to a 302 status code.

also, my suggestion referred to this:
...the search engines still have the old location-game-person and I think I will be getting duplicates for this.

So should I now have a rewrite for the dashes to slashes?

example.com/location-game-person

to

example.com/location/game/person

you should 301 redirect your legacy "dash" urls to the new "slash" urls.
the internal rewrites to the namesearch script should handle the "slash" urls.
12:46 am on Mar 30, 2019 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15934
votes: 889


Matter of fact, you can also consolidate in the rewrites:
RewriteRule ^(north|south)/(baccarat|bridge)/(.*)$ /php/namesearch.php?location=$1&game=$2&person=$3 [L]
Performance metrics on this scale are so narrow that you might as well use whatever form you, personally, are most comfortable with. Since you’ve got a series of mutually exclusive rules, each with an [L] flag, try to list them in order of most-likely-to-succeed: If more people play north than south (but, but, what about east and west?) then put the ^north rules first, and similarly if bridge is more popular than baccarat, put the bridge rules first.

:: trying to avoid asking why location trumps game type ::

Overall your rules are in the right order, so don’t change that part: first the external redirects--with, as phranque says, added R=301 flag--and then the internal rewrites. (“External” doesn’t necessarily mean go to some other site. It just means you’re sending information back to the visitor, instead of furtively doing stuff within your own server.)

If you’ve changed format, as from - to /, you need to institute redirects so you’re not left dealing with two forms of every URL.
10:16 am on Mar 31, 2019 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 17, 2002
posts: 1189
votes: 6


Points noted and applied. All looks fine now. Redirects are working and no 404s.