Forum Moderators: phranque

Message Too Old, No Replies

301 Redirect Rules Help

         

gbrown442

10:00 pm on Sep 9, 2012 (gmt 0)

10+ Year Member



Hi, looking for some help with some redirect rules, below is what I'm trying to do and what I've tried:

http://www.example.com/subfolder/

TO

http://www.example.com/AAA/BBB/subfolder/

TRIED

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^www.example.com/subfolder/
RewriteRule ^(.*)$ www.example.com/AAA/BBB/subfolder/$1 [L,R=301]

Thanks in advance!

g1smd

11:19 pm on Sep 9, 2012 (gmt 0)

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



HTTP_HOST can only match a hostname and no other part of the URL. Why are your checking this variable anyway? What do you want to happen when someone requests the non-www version?

The (.*) will redirect all pages of the site. Change this to include only those you do what to redirect.

Are you sure you really need the -f and -d checks?

lucy24

12:03 am on Sep 10, 2012 (gmt 0)

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



The (.*) will redirect all pages of the site.

More exactly-- and I think this is what you meant-- it will redirect all FILES on the site. Including images, stylesheets, javascript and so on. Things that generally wouldn't need redirecting, because how many people have your stylesheets bookmarked? (Same applies to most 403s. If they've got permission to see the page itself, it is generally safe to assume they're approved for all the subsidiary files, so no need to check for those.)

gbrown442

2:57 am on Sep 10, 2012 (gmt 0)

10+ Year Member



Yeah, I may be doing this wrong. I've tried this now and still getting errors:

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteRule ^denver-broncos/(.*) www.example.net/nfl/teams/denver-broncos/$1 [L,R=301]
RewriteRule ^denver-broncos/2010/(.*) www.example.net/nfl/2010/$1 [L,R=301]
RewriteRule ^denver-broncos/2011/(.*) www.example.net/nfl/2011/$1 [L,R=301]
RewriteRule ^denver-broncos/2012/(.*) www.example.net/nfl/2012/$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

lucy24

4:01 am on Sep 10, 2012 (gmt 0)

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



RewriteRule ^denver-broncos/(.*) www.example.net/nfl/teams/denver-broncos/$1 [L,R=301]
RewriteRule ^denver-broncos/2010/(.*) www.example.net/nfl/2010/$1 [L,R=301]
RewriteRule ^denver-broncos/2011/(.*) www.example.net/nfl/2011/$1 [L,R=301]
RewriteRule ^denver-broncos/2012/(.*) www.example.net/nfl/2012/$1 [L,R=301]

Rules 2 through 4 are contained within Rule 1, so they will never execute. Basic Principle: Go from the most specific to the least specific. So you start with rules 2-4, which can be conflated into one:

RewriteRule ^denver-broncos/(201[012]/.*) http://www.example.net/nfl/$1 [L,R=301]

(not just domain name but full protocol)

That's assuming for the sake of discussion that your site's nfl contains only one team. But I'm inclined to suspect that it really goes something more like

http://www.example.net/teamname/year/optionalblahblah

and, if so, you'll probably have something more like

RewriteRule ^(team1|team2|team3|etcetera)/(201[012]/.*) http://www.example.net/nfl/$1/$2 [L,R=301]

You could include the directory slashes in your captures. I left them out because it helps keep a grip on the structure.

gbrown442

4:11 am on Sep 10, 2012 (gmt 0)

10+ Year Member



Ah alright, that makes sense on the logic part.

And you are correct, there are multiple teams! :) There are also multiple leagues, will that work the same way?

Lastly, since that will be a very long line with all the teams. Is it suggested that we break those into maybe 3-5 lines for each league or keep it one long line?

Just to confirm, here's the different redirects we're looking at:

example.net/denver-broncos/

to

example.net/nfl/teams/denver-broncos/

AND

example.net/denver-broncos/year/month/date/permalink/

to

example.net/nfl/year/month/date/permalink/

Thanks!

lucy24

6:45 am on Sep 10, 2012 (gmt 0)

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



There are also multiple leagues, will that work the same way?

Sure, just keep each set of rules clumped together.

blahblah/(team1|team2|team3)/moreblahblah
redirects to
blahblah/nfl/$1 et cetera

blahblah/(team4|team5|team6)/moreblahblah
redirects to
blahblah/ncaa/$1 et cetera

Yes, you are right. I am a little out of my-- haha-- league when it comes to football. But you get the idea.

Lastly, since that will be a very long line with all the teams. Is it suggested that we break those into maybe 3-5 lines for each league or keep it one long line?

I don't know if it makes any significant difference in execution time. But it would definitely help your sanity to keep them in smaller groups. There's a finite number of teams, so you could make up packages-- alphabetic, geographic, whatever suits your fancy-- and always work with the same package.

example.net/denver-broncos/year/month/date/permalink/

to

example.net/nfl/year/month/date/permalink/

So in this version you really are dumping the team name? Not that it really makes much difference: You either capture them or you don't. The parentheses will be there either way because of your pipe-separated groups:

blahblah/(team1|team2|team3)/moreblahblah

If you're not reusing the team name you can make those parentheses non-capturing to save a teeny bit of server overhead:

blahblah/(?:team1|team2|team3)/moreblahblah