Forum Moderators: phranque

Message Too Old, No Replies

Similar RewriteRules problem

         

amberleaf1

2:04 pm on Jan 1, 2014 (gmt 0)

10+ Year Member



Hey all, I hope someone can help me a 100% total noob when it comes to htacces, with my problem.


RewriteRule ^/?show/([a-zA-Z0-9\-]+)/season/([0-9]+)/episode/([0-9]+)$ /?([a-zA-Z0-9\-]+)/season/([0-9]+)/episode/([0-9]+) [L]

I have a tv and movie website and the above rule in my htacces file give me links that look like this

mysite/show/showname/season/1/episode/1

Which are the type of links I want and I'm happy with.

BUT, for some reason there was also the following rule,

RewriteRule ^/?([a-zA-Z0-9\-]+)/season/([0-9]+)/episode/([0-9]+)$ index.php?menu=episode&perma=$1&season=$2&episode=$3 [L]

That gave me the following link,

mysite/showname/season/1/episode/1

which I don't want, but various search engines have indexed both type of links so no doubt now they're penalising the site for duplicate content.

So, my question is. How do I redirect this mysite/showname/season/1/episode/1 to this mysite/show/showname/season/1/episode/1

Please also be aware that I have over 1400 tv shows in my database.

Thanks

g1smd

6:50 am on Jan 3, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRules don't "give you links". The links visible on the page are created by the PHP code that creates the page of content. Make sure that those links all point to the correct URL format. Edit the PHP to ensure the correct links are produced.

The first rule cannot possibly work. The syntax is completely wrong. Delete it.

The second rule matches incoming URL requests beginning with the showname and internally rewrites that request to point to the PHP script and pass the correct parameters to it.

What other RewriteRules are there in the file?

The second rule will need the word "show" inserted at the beginning to match incoming URL requests that begin with the word "show".

To redirect incorrect requests to the right URL, you will need another RewriteRule. This extra rule will redirect requests without the word "show" to the correct URL with the word "show":

# Redirect without "show" to with "show"
RewriteRule ^([a-zA-Z0-9\-]+)/season/([0-9]+)/episode/([0-9]+)$ http://www.example.com/show/$1/season/$2/episode/$3 [R=301,L]


It must be placed near the top of the htaccess file, before your non-www to www redirect.

Important! Rules that redirect must be listed before any rules that rewrite.

Place a blank line after every RewriteRule so that the code is easier to read.

Make sure that each rule has a preceding comment describing what it does.

lucy24

7:44 am on Jan 3, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Overlapping g1smd, who posted almost an hour ago so I've really got no excuse

RewriteRule ^/?show

In htaccess there is no leading slash. Is the question mark because you weren't sure? You can delete the whole /? package.

RewriteRule ^/?show/([a-zA-Z0-9\-]+)/season/([0-9]+)/episode/([0-9]+)$ /?([a-zA-Z0-9\-]+)/season/([0-9]+)/episode/([0-9]+) [L]

That is: internally rewrite requests for
example.com/show/blahblah/season/otherblahblah/episode/finalblahblah
to
example.com/?blahblah
...
Now, wait a minute. That makes no sense. What I'd expect to see is something like

RewriteRule ^show/([a-zA-Z0-9\-]+)/season/([0-9]+)/episode/([0-9]+)$ /index.php?show=$1&season=$2&episode=$3


RewriteRule ^/?([a-zA-Z0-9\-]+)/season/([0-9]+)/episode/([0-9]+)$ index.php?menu=episode&perma=$1&season=$2&episode=$3 [L]

Well, there you go. That's essentially what one would expect, barring the "menu=episode" which I suppose is used by your CMS or template.

for some reason there was also the following rule

Awright, come clean. Rules don't just materialize. Have you taken over someone else's site? Or did your developer up and leave so you're stuck making sense of the code?

Incidentally, your URLs have an awful lot of slashes. If the site is already established there's no point in changing them. But otherwise, it seems like all you'd need is something like
www.example.com/show/show-name-here/season123/episode123

How do I redirect this
/showname/season/1/episode/1
to this
/show/showname/season/1/episode/1

When you say "over 1400 shows" do you mean 1400 different shows, so the "showname" element at the front of some URLs could be any of 1400 different things? Does anything else fit the pattern of
some-stuff-here/season/123/episode/123
? If not, it's one of the easiest redirects to code. Show us your best effort. Contrariwise: If there are piles of other URLs fitting the pattern that are not to be redirected, then don't even try to do it in htaccess. Hand it off to your existing php script.

amberleaf1

2:02 pm on Jan 3, 2014 (gmt 0)

10+ Year Member



Thanks for the replies guys,

As I said I am a NOOB when it comes to the htaccess page, but I'm learning by the seat of my pants.

Just to point out I didn't write the script I'm using for my site, that was the job of my brother who was the code writer. And I sort of inherited it when he... well.

Anyway, after going through all my script code with a fine tooth comb, to understand how it creates the links. ( It was a mess ) Then finding out what type of links the search engines are finding.
( With the "show" or without).
I decided to go a different way and delete anything to do with having the word "show" in my links and amended the PHP as required and removed from the htacces file any rule that created SEO friendly links with the word "show" in it.

So now the links are mysite/showname/season/1/episode/1
and not mysite/show/showname/... etc

And added

RewriteRule ^show/(.*)$ /$1 [R=301,NC,L]

to redirect any search engine indexed links to my chosen style.

lucy24

8:24 pm on Jan 3, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteRule ^show/(.*)$ /$1 [R=301,NC,L]

See? I SAID it was one of the easiest rules to write :) Now fine-tune it to

RewriteRule ^show/(.*)$ http://www.example.com/$1 [R=301,NC,L] 


with full protocol-plus-domain (substitute https if that's what you use) and you're good to go.

The [NC] flag isn't strictly necessary, but will do no harm in a redirect. Well, except that it will slow your server a teeny, tiny, infinitesimal bit because it's now searching for [Ss][Hh][Oo][Ww].

g1smd

6:28 pm on Jan 4, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There was a benefit in valid URLs beginning with the word "show".

If you wanted to add some other type of page at a later date, all those URLs would begin with some other word.

lucy24

6:52 pm on Jan 4, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There was a benefit in valid URLs beginning with the word "show".

Yes, it's one of the rare cases where a seemingly superflous piece of URL really was appropriate. But the most important thing is to reduce all requests to a single form. Which form to use can always be tweaked.

If you have a page about, um, "Show of Shows" or "Showtime" et cetera, try to give the server a break by making the page name begin with something other than "show" ;)