Forum Moderators: phranque

Message Too Old, No Replies

Redirect all pages in different subfolders to the subfolder

         

zeus

9:37 pm on Apr 8, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sounds a little weird, but I need all pages that are in the subfolder to 301 to the subfolder. like this

category/bla redirect to category/
category2/burb redirect to category2/
category3/also redirect category3/

there are many pages in each folder and a lot of folders.

lucy24

10:42 pm on Apr 8, 2012 (gmt 0)

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



A little weird? I was going to say it sounds a little suicidal, but it's your site ;)

Coding the redirect is not hard. But you need to make sure you're not redirecting material in folders that are not supposed to be redirected. There will always be a few. Depending on the actual numbers, you will either have a line that says

"folder IS {anything in this list}"

or a line that says

"folder IS NOT {anything in this list}"

Not in those words, of course. It will really start with "RewriteCond..." But that comes later. Step 1 is to work out, in English, every detail of what you plan to do.

Are we talking about top-level folders? That is:

www.example.com/folder
www.example.com/otherfolder
www.example.com/onemorefolder

or some other structure? Start by figuring out what all your directories have in common, so you can get them into the smallest possible number of rules.

zeus

10:54 pm on Apr 8, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello Lucy - ok I do this be cause I think this can be a panda solution. the site is like this

example.com/folder/category/page we then have a lot of different categories.

When we look on server its folder/public_html/folder/albums/category/

would be great if we have a solution here on ww be cause I have really searched the net, ok google is not what it has been when searching for something in that category, but I also used bing

lucy24

10:23 am on Apr 9, 2012 (gmt 0)

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



So it's the /category/ folders inside a higher-level /folder/ that you want to redirect? Whew. That seems straightforward.

I hope you are not saying that you want the redirect to be based on where the folders "really" live on your server, as opposed to where they show up in the user's address bar. If yes, then we will both have to sit back and wait for a grownup, because I'm not touching that one with a barge pole.

Hmm... If you were a brand-new member you would now get the lecture about "let's see what you've got so far because we're not here to write your code for you". But if you've been around since 2002 you have probably heard that about 45 times already. And have earned the right to say I Don't Do Apache.

Off the top of my head because it's 3AM, so do not repeat do not cut & paste :)

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

I used .+ because there has to be something after the folder name. Otherwise there would be an infinite redirect if the user only asked for /folder/category/ in the first place. As written, it will also pick up an explicit "index.html" or equivalent-- but that is a good bonus, because you would want to redirect those anyway.

If there are any folders inside that top-level folder that you don't want to redirect, there will need to be a condition that says something like

%{REQUEST_URI} !(notthisone|andnotthis)/

And... ahem. See above about 3AM. Do the /folder/category/ folders contain any non-page material, like images or stylesheets or javascript files? If so, you will need to fine-tune the wording of the Rule so that you're only redirecting pages. (This may be the only .htaccess mistake I have never made. But it's early days yet.)

zeus

11:10 am on Apr 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for the reply, I have been here a long time and any questions regarding seo on images or search, I know what im talking about, but apache, redirecting thats my weak spot.

Your code would you have me to place that in each folder?
example.com/folder/category1/page so as example this have to redirect to example.com/folder/category1/ and of those categories there are 1000 different and yes we only need to make it look good in the url and for google.

lucy24

4:40 pm on Apr 9, 2012 (gmt 0)

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



No, you just make the rule once and put it in your primary htaccess. Or your config file if you've got one. (Something tells me you don't.) You could put the rule one level further down, in the /folder/ containing all those subfolders, but in general it is safer to keep everything in a single htaccess file.

In any case you only want to redirect requests for pages; that's why I asked if you have any images etc within that same /folder/.

#1 If images are scattered all over the place:

RewriteRule ^(folder/[^/]+/)[^.]+\.html http://www.example.com/$1 [R=301,L]

replacing "html" with whatever your extension really is. If you are already extensionless, express the non-capturing part as [^.]+$ You need the ending anchor so the RegEx doesn't just capture /folder/category/picture ... and then say "Extension? What extension? I don't see any extension."

#2 If the /folder/ has sub-folders that contains all the non-page files, you could also do it with a Condition:

RewriteCond %{REQUEST_URI} !/(images|styles|scripts)/

OK... What have you got so far? ;)

zeus

6:39 pm on Apr 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteRule ^(folder-where-the-category-folders-are/[^/]+/)[^.]+$ http://www.example.com/$1 [R=301,L] example is my domain, I placed it in the main folder where the category holder folder is, thats the best way for this site, thats also where i do redirects on another site, nothing els is possible. Now it did not work the version I showed you here, also the other mentioned at your last post

zeus

7:03 pm on Apr 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hmm could I trick it so RewriteRule ^category1 http://www.example.com/category1/ [R=301,L] that would redirect everything in the folder to the category page or not, mostly i use this to redirect to another part of the site. the dumb thing is would have to do this for each category

lucy24

9:08 pm on Apr 9, 2012 (gmt 0)

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



^(folder-where-the-category-folders-are/[^/]+/)[^.]+$

This version will only capture requests for extensionless files. If your pages end in html or php or whatever it may be, then you need to include the extension:

^(folder-where-the-category-folders-are/[^/]+/)[^.]+\.html$

zeus

9:20 pm on Apr 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



no extensions looks like folder/category1/page

lucy24

1:29 am on Apr 10, 2012 (gmt 0)

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



Uh-oh, then it should work. Do you have existing rewrites in your htaccess? If not, you will need to add the line

RewriteEngine on

anywhere before the first Rewrite. Unlike most things in Apache, this line needs to be repeated in each htaccess file that uses mod_rewrite.

Of course you've cleared your browser cache, tried a different browser and that kind of thing. Right? Everyone gets bitten by that one sooner or later. You think it isn't working, and then you refresh and tralala, everything comes through as intended.