Forum Moderators: phranque

Message Too Old, No Replies

redirect old links

         

bernte

11:11 pm on Jul 27, 2008 (gmt 0)

10+ Year Member



hi guys! i build a little htaccess wich also works.

RewriteEngine On
Options +FollowSymlinks
RewriteBase /test/

### ausgeschlossene Ordner
RewriteRule ^css/ - [L]
RewriteRule ^img/ - [L]

### 301 Weiterleitung auf WWW
RewriteCond %{HTTP_HOST} !^www.example\.com$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]

###RewriteCond %{REQUEST_URI} ^/[^\.]+[^/]$
###RewriteRule [^/]$ %{REQUEST_URI}/ [R=301]
###RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]

# ohne Schrägstrich am Ende
RewriteRule (.+)/$ /test/$1 [L,R=301]

RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&i=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?lang=$1&i=$2&cat=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/item/([^/]+)/?$ index.php?lang=$1&i=$2&cat=$3&item=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?lang=$1&i=$2&cat=$3&fab=$4 [L]

my old linkstructure was in this way... www.example.com/index.php?lang=de&i=about
the new structure is in this way.. www.example.com/test/de/about

the "test" in the link is the current place for testing...

to this way everything folds. but i want my old links which are indexed by google redirect to the new url! because of DC and PR.

now i reach my site still over the old link structure like this example
www.example.com/test/index.php?lang=de&i=about

which i dont want! the old link should be directed to
www.example.com/test/de/about via 301

I would be pleased if me someone to help could please!
sorry for my bad english! hope you understand what i mean!

best regards

bernte

[edited by: jdMorgan at 11:22 pm (utc) on July 27, 2008]
[edit reason] example.com [/edit]

g1smd

12:33 am on Jul 28, 2008 (gmt 0)

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



You need to make sure that your more specific redirects are first in the code, and then follow on with the more generally-reaching redirects. The catch-all for all non-www should be the very last. Every redirect should enforce the canonical domain too.

After all of the redirects are listed, you should have your rewrites, again, from most specific through to most general.

bernte

9:00 am on Jul 28, 2008 (gmt 0)

10+ Year Member



hello g1smd thanks for your fast answer! :)
i dont know if i understand you right.. do you mean that?
i changed the code to this

RewriteEngine On
Options +FollowSymlinks
RewriteBase /test/

### barred folders
RewriteRule ^css/ - [L]
RewriteRule ^img/ - [L]

### 301 redirect to WWW
RewriteCond %{HTTP_HOST} !^www.example\.com$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]

# no slash at last
RewriteRule (.+)/$ /test/$1 [L,R=301]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/item/([^/]+)/?$ index.php?lang=$1&i=$2&cat=$3&item=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?lang=$1&i=$2&cat=$3&fab=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?lang=$1&i=$2&cat=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&i=$2 [L]

is it that what you mean?

i think my links are all canonical

and what do you mean with "The catch-all for all non-www should be the very last."
do you mean that i have to put this


### 301 redirect to WWW
RewriteCond %{HTTP_HOST} !^www.example\.com$
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]

to the bottom of the htaccess? or what?

and what is with my other problem?

now i reach my site still over the old link structure like this example
www.example.com/test/index.php?lang=de&i=about

which i dont want! the old link should be directed to
www.example.com/test/de/about via 301


is there a way 2 fix that?

regards berni

bernte

9:34 am on Jul 28, 2008 (gmt 0)

10+ Year Member



oh damn.. i forgot something...
when i go to the site www.example.com/test/de i become an 404..
that should be right because i forgot the rule to get access to this area... so i added it (the last rule)

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/item/([^/]+)/?$ index.php?lang=$1&i=$2&cat=$3&item=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ index.php?lang=$1&i=$2&cat=$3&fab=$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?lang=$1&i=$2&cat=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?lang=$1&i=$2 [L]
RewriteRule ^([^/]+)$ index.php?lang=$1 [L]

now that i got access to the site www.example.com/test/de
but the problem is when i click on a link which is linked hard (like that way <a href="http://www.example.com/test/de/about">)
from the area www.example.com/test/de to www.example.com/test/de/about or other links, the site stays in area www.example.com/test/de but in the adressbar stands www.example.com/test/de/about.
when i kick the last rule everything works again in all areas when i start direct from here www.example.com/test/de/about or here www.example.com/test/
but i think i can also start from /test/ because the folder is my "root" and really exists and the index.php is in this folder.

has anybody also an idea to this problem?

thanks for your help!
best regards berni

jdMorgan

2:22 pm on Jul 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You'll do better to work on one problem at a time in this forum context.

To get started on finding the answer to your original question, see this recent thread [webmasterworld.com].

Jim

g1smd

6:24 pm on Jul 28, 2008 (gmt 0)

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



# no slash at last
RewriteRule (.+)/$ /test/$1 [L,R=301]

This rule does not enforce the www and as it is more specific it should be before your non-www to www redirect.

It needs to also enforce www within the rule, rather than chaining with some other rule.

.

You also have a rewrite at the very start of the code, but you should place all your rewrites after all of the redirects.

bernte

8:51 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



he guys thanks for your help!
now it works!
but i have an new error :D

i dont know how i can explain because my english is not very good but i will try!

look at this code
RewriteRule ^test/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /test/index.php?lang=$1&i=$2&cat=$3&item=$4 [L]
RewriteRule ^test/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /test/index.php?lang=$1&i=$2&cat=$3&fab=$4 [L]

the problem is that just works the first rule!
i think the problem is that the first and second rule has the same code just the end with item and fab is the difference.

the fab in the 2nd rule is just needed for on area
like this.. &cat=$3 is always cat=manufaturer&fab=$4

can somebody tell me how it works?

best regards

bernte

g1smd

9:03 pm on Aug 14, 2008 (gmt 0)

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



Yes, for the second rule to work, the first rule must "select" only specific URLs to be rewritten, allowing the rest to pass through to be used by the second rule.

When I needed to do something like this before, I was lucky that for one parameter, the value could be either four digits or seven digits. That made it easy to select out which rule needed to be used.

jdMorgan

9:33 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Remember, the rules are activated by matching the regular-expressions pattern on the left side of the Rule to the incoming URL request. So, the best you can do is to reverse the order of the two rules, and require "manufacturer" as the third URL-path component in the incoming request:

RewriteRule ^test/([^/]+)/([^/]+)/manufacturer/([^/]+)$ /test/index.php?lang=$1&i=$2&cat=manufacturer&item=$3 [L]
RewriteRule ^test/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /test/index.php?lang=$1&i=$2&cat=$3&item=$4 [L]

You may need to further qualify this by requiring specific values in the fourth URL-path component (fab), or by making the on-page URLs more different to avoid this ambiguity.

Jim

bernte

10:25 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



thank you jim! it works! i spend so much time with this! THANKS!

bernte

5:40 pm on Aug 23, 2008 (gmt 0)

10+ Year Member



hey guys! its me again :D *sorry* :D

now on weekend i wanted to get online with the modrewrited site from my uncle.. but i got an new error! and i have no idea why.
the problem are the subdirs in my testfolder
before i had the problem some post on top i got access to the test/css/ and test/img/ folder

now i saw that it doesnt work anymore!

when i go to http://www.example.com/test/css the page moves to http://www.example.com/test/css?lang=css and i become an error in firefox!
i try to translate the error:

Error: forwardingerror
The inquiry reroutes the called website in such a way that it can be never terminated.

could somebody take a look again at my code please?


RewriteEngine On
Options +FollowSymlinks

### 301 redirect to WWW
#RewriteCond %{HTTP_HOST} !^www.example\.com$
#RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]

# no slash at last
RewriteRule ^test/([^/]+(/[^/]+)*)/$ /test/$1 [L,R=301]

### freigegebene ordner
RewriteRule ^test/css/ - [L]
RewriteRule ^test/img/ - [L]

### tatsaechliche files ausschliessen
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^test/index\.php$ - [L]

RewriteRule ^test/([^/]+)/([^/]+)/fabricant/([^/]+)$ /test/index.php?lang=$1&i=$2&cat=fabricant&fab=$3 [L]
RewriteRule ^test/([^/]+)/([^/]+)/fabricante/([^/]+)$ /test/index.php?lang=$1&i=$2&cat=fabricante&fab=$3 [L]
RewriteRule ^test/([^/]+)/([^/]+)/hersteller/([^/]+)$ /test/index.php?lang=$1&i=$2&cat=hersteller&fab=$3 [L]
RewriteRule ^test/([^/]+)/([^/]+)/manufacturer/([^/]+)$ /test/index.php?lang=$1&i=$2&cat=manufacturer&fab=$3 [L]
RewriteRule ^test/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /test/index.php?lang=$1&i=$2&cat=$3&item=$4 [L]

## shippingform - vorn und hinten aendern
RewriteRule ^test/([^/]+)/estimation_envoi/([^/]+)$ /test/index.php?lang=$1&i=estimation_envoi&item=$2 [L]
RewriteRule ^test/([^/]+)/gastos_envio/([^/]+)$ /test/index.php?lang=$1&i=gastos_envio&item=$2 [L]
RewriteRule ^test/([^/]+)/versandanfrage/([^/]+)$ /test/index.php?lang=$1&i=versandanfrage&item=$2 [L]
RewriteRule ^test/([^/]+)/shipping_request/([^/]+)$ /test/index.php?lang=$1&i=shipping_request&item=$2 [L]

## orderform - vorn und hinten aendern
RewriteRule ^test/([^/]+)/commande/([^/]+)$ /test/index.php?lang=$1&i=commande&item=$2 [L]
RewriteRule ^test/([^/]+)/orden_form/([^/]+)$ /test/index.php?lang=$1&i=orden_form&item=$2 [L]
RewriteRule ^test/([^/]+)/bestellformular/([^/]+)$ /test/index.php?lang=$1&i=bestellformular&item=$2 [L]
RewriteRule ^test/([^/]+)/orderform/([^/]+)$ /test/index.php?lang=$1&i=orderform&item=$2 [L]

RewriteRule ^test/([^/]+)/([^/]+)/([^/]+)$ /test/index.php?lang=$1&i=$2&cat=$3 [L]
RewriteRule ^test/([^/]+)/([^/]+)$ /test/index.php?lang=$1&i=$2 [L]
RewriteRule ^test/([^/]+)$ /test/index.php?lang=$1 [L]

ErrorDocument 403 /test/error/403.php
ErrorDocument 404 /test/error/404.php

best regards berni

g1smd

9:42 pm on Aug 23, 2008 (gmt 0)

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



Somewhere you need a RewriteCond that says to not do the rewrite for those specific URLs.

bernte

1:16 pm on Aug 24, 2008 (gmt 0)

10+ Year Member



hi g1smd
i don't know if u understand me right..

the website with the rules works fine.. just when i want manually go to an existing subfolder which really exists comes the error..

when u understand me right and i must create a rewritecond i dont know how :(

i did it with the code in line 11

### freigegebene ordner
RewriteRule ^test/css/ - [L]
RewriteRule ^test/img/ - [L]

can you tell me what i have to do to get access to these folders?

jdMorgan

2:24 pm on Aug 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your "by-pass" rules require a trailing slash, and won't work if you request these resources without the slash. Make the slashes optional in your patterns:

### freigegebene ordner
RewriteRule ^test/cs[b]s/?[/b] - [L]
RewriteRule ^test/im[b]g/?[/b] - [L]

Jim

bernte

3:22 pm on Aug 24, 2008 (gmt 0)

10+ Year Member



hey jim
i dont know if i understand you right!
my rule is now changed into this

### freigegebene ordner
RewriteRule ^test/css/ - [L]
RewriteRule ^test/css - [L]
RewriteRule ^test/img/ - [L]
RewriteRule ^test/img - [L]

when i now try to reach
http://www.example.com/test/css the site dont moves to http://www.example.com/test/css?lang=css

it stays right at
http://www.example.com/test/css

the same with
http://www.example.com/test/css/ the site dont moves to http://www.example.com/test/css/?lang=css

it stays right at
http://www.example.com/test/css/

but the error-message in firefox is still the same :(

Error: forwardingerror
The inquiry reroutes the called website in such a way that it can be never terminated.

jdMorgan

4:04 pm on Aug 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Enter the code exactly as I typed it above -- Two rules, not four.

Then completely flush your browser cache, and see if that helps.

Jim

bernte

4:17 pm on Aug 24, 2008 (gmt 0)

10+ Year Member



sorry jim i understand you wrong! ok i fixed it how you wrote it!
but its the same .. no moving to /?lang=css but the error is still there

i check this folder with [web-sniffer.net...]

when i check
http://www.example.com/test/img comes
HTTP Status Code: HTTP/1.1 301 Moved Permanently to
http://www.example.com/test/img/

when i check it on the other way
http://www.example.com/test/img/ comes
HTTP Status Code: HTTP/1.1 301 Moved Permanently to
http://www.example.com/test/img

i think thats it why comes the error in firefox.. its an never ending loop

is it my rule that makes the error on line 8
# no slash at last
RewriteRule ^test/([^/]+(/[^/]+)*)/$ /test/$1 [L,R=301]

i create this rule that i become no dublicate content!
and for all the sites it works...
example:
when i go to http://www.example.com/test/en/about/
site moved to http://www.example.com/test/wn/about
but i dont want it for the real folders

g1smd

4:18 pm on Aug 24, 2008 (gmt 0)

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



JD highlighted the bit that you need to change *in* the example.

jdMorgan

4:34 pm on Aug 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, correct and reverse the order of your first two rules. The more-specific redirect should be first:

# no slash at last
RewriteRule ^test/(.+)/$ [b]http://www.example.com[/b]/test/$1 [R=301,L]
#
# 301 redirect to WWW
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Jim

bernte

4:51 pm on Aug 24, 2008 (gmt 0)

10+ Year Member



ok i moved #no slash at last on top! ( i used your rule not my rule)
the #301 redirect to WWW is not active at this moment because the old site is stil online! i hope thats not the problem

but it changed noting.. i got the same result with web-sniffer.

[edited by: jdMorgan at 5:01 pm (utc) on Aug. 24, 2008]
[edit reason] No URLs, please. [/edit]

jdMorgan

5:00 pm on Aug 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might want to look for other rules in /test/.htaccess or in your server configuration files. There is another rule or possibly code in your /index.php PHP script that is generating a second redirect to add the slash back on after you redirect to remove it.

Jim

jdMorgan

5:01 pm on Aug 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And flush your browser cache completely before testing any changes to your code...

Jim

bernte

5:07 pm on Aug 24, 2008 (gmt 0)

10+ Year Member



in my testfolder is no .htaccess file.. the file is just in the root of my server.
in my index.php i have just a redirect for this

http://www.example.com/test/about ok
http://www.example.com/test/uwduwdwudh not ok than moved to http://www.example.com/test/

sorry for link posting above

bernte

5:14 pm on Aug 24, 2008 (gmt 0)

10+ Year Member



my cache is always deleted when i change something

jdMorgan

5:35 pm on Aug 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> http://www.example.com/test/about ok
> http://www.example.com/test/uwduwdwudh not ok than moved to http://www.example.com/test/

That looks like a problem in two ways. First, it looks like /test/index.php and /test/css might be "not OK" and could be redirected.

Second, this error-handling method is very bad for search engines; The method should be, "If not OK, then issue 404-Not Found server response, and generate 404 error page." The 404 error page should then contain text links to your home page, site map, site search page, etc. You should never redirect (301, 302, 303, or 307) errors to your home page, because this can create massive duplicate-content propblems -- You should always send the proper error response and output a "helpful" page for the user.

Jim

bernte

5:43 pm on Aug 24, 2008 (gmt 0)

10+ Year Member



ok i try to turn off the redirect in the index.php

i created an empty index.php and put it into the test/img/ folder...
the problem is that it also doesnt work! just when i manually type test/img/index.php it works..

is it the problem with the index.php redirect? or is it another problem?

jdMorgan

3:59 pm on Aug 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You cannot test in the /img folder, because your rules are bypassed for that folder and the /css folder, and will not be applied. These are the two rules which prevent /index.php rewrites in /img and /css:

### freigegebene ordner
RewriteRule ^test/css/? - [L]
RewriteRule ^test/img/? - [L]

These two rules are correct, but they mean that you cannot use that subdirectory for testing the other rules.

Jim

bernte

9:43 pm on Aug 25, 2008 (gmt 0)

10+ Year Member



hey jim thanks for your answer.
is there a way that i get access to my folders?
its not only the img and css folder.. i have another important folder i need to handle

regards berni

bernte

5:51 pm on Aug 27, 2008 (gmt 0)

10+ Year Member



he guys now it works fine with my folders

here is my code
ErrorDocument 403 /test/error/403.php
ErrorDocument 404 /test/error/404.php

RewriteEngine On
Options +FollowSymlinks

### freigegebene ordner
RewriteRule ^test/css/? - [L]
RewriteRule ^test/img/? - [L]

now i have another question :D
is that the only way to get access to the folders?

for example:
the site is only and i create ne new folder in the root pub or something other... must i now write every folder in the htaccess?

because when i go now to test/css/ or test/img/ i become an 403 <-- thats right beause there is no file inside!
but when i go for example to test/#*$!/ which folder realy exists but is not write down in the htaccess i become again the forwarding error.

or is there another error?

i want to say 1000 thanks to all the helpers

berni