Fabbio
Fabbio

Reputation: 363

Preventing access to seo-unfriendly URLs and redirect to seo-friendly (canonical) URLs

I see many similar questions but none helped me so far:

I have the logic of my script under /song/song.php?url=lang/params (seo-unfriendly URL). I use this rewrite to have a friendly URL and load the content from the seo-unfriendly one, using a parameter to differentiate between languages:

 RewriteRule ^songs-and-chords/(.*?)(/*)?$ /song/song.php?url=en/$1 [NC,L]
 RewriteRule ^canzoni-e-accordi/(.*?)(/*)?$ /song/song.php?url=it/$1 [NC,L]

Now I want to prevent access to /song/song.php, forcing a redirect to the seo-friendly one, so something like this, using the %{THE_REQUEST} to make external redirect:

 RewriteCond %{THE_REQUEST} .*?song/song\.php\?url=it/(\S*) [NC]
 RewriteRule /canzoni-e-accordi/$1 [R,L]
 RewriteCond %{THE_REQUEST} .*?song/song\.php\?url=en/(\S*) [NC]
 RewriteRule /songs-and-chords/$1 [R,L]
 RewriteCond %{THE_REQUEST} ^.*?song/song\.php.*$ [NC]
 RewriteRule /songs-and-chords/ [R,L]

But non of this rules is executed... What am I doing wrong? How can I debug this in a good way?

(I already use rel="canonical" in the song.php script but still I would prefer the script to be not accessible at all).

Upvotes: 1

Views: 169

Answers (1)

hjpotter92
hjpotter92

Reputation: 80639

What you're doing wrong is, the pattern of your RewriteRule statements is missing. Also, the rewritten URLs would be using %1 reference and not $1.

RewriteCond %{THE_REQUEST} ^GET\ /song/song\.php\?url=it/(\S+) [NC]
RewriteRule ^ /canzoni-e-accordi/%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /song/song\.php\?url=en/(\S+) [NC]
RewriteRule ^ /songs-and-chords/%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /song/song\.php\s [NC]
RewriteRule ^ /songs-and-chords/ [R=301,L]

Upvotes: 1

Related Questions