Forum Moderators: phranque

Message Too Old, No Replies

After Reading the Charter

         

CainIV

6:35 am on Aug 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello guys, after reading the charter, I cannot find a way to redirect a dynamic url with a 301 to a static url that already exists but is in a different directory for example:

redirect:

[somesite.com...]

to

[somesite.com...]

Here is what I have treed:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*home\.php?cat=255\ HTTP/
RewriteRule ^(.*)home\.php$ [somesite.com...]
[R=301,L]

Any thoughts on what I might be doing wrong are appreciated...

Todd

jdMorgan

3:57 pm on Aug 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd suggest:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*home\.php?cat=255\ HTTP/
RewriteRule ^(([^/]+/)*)home\.php$ http://www.example.com/[b]$1[/b]thenewstaticpage.ht[b]ml?[/b] [R=301,L]

The regular expressions pattern shown should be faster to process than the ambiguous ".*" pattern, which would require n 'tries' in order to match, where 'n' is the character length of the pattern that follows ".*"

The pattern (([^/]+/)*) means "Match zero or more instances of (one or more characters not equal to a slash, followed by a slash), and store all such instances in $1." By looking for a specific condition to end the match (a slash), the pattern-matching is more efficient.

Because no back-reference to the RewriteCond pattern match is used, the second set of parentheses is not needed in the RewriteCond pattern.

Jim

CainIV

5:29 pm on Aug 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thankyou Jim, making it more efficient makes total sense.

I however cannot get the code to redirect for me. Here is the existing htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{REQUEST_URI} ^/?$ [OR]
RewriteCond %{SERVER_PORT}!443
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html\ HTTP/
RewriteRule ^(.*)index\.html$ http://www.example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*home\.php\ HTTP/
RewriteRule ^(.*)home\.php$ http://www.example.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*home\.php?cat=255\ HTTP/
RewriteRule ^(([^/]+/)*)home\.php$ http://www.example.com/$1thenewpage.html? [R=301,L]

Should there be a question mark at the end of the html page on the last line of code? Also, can you explain to me why the $1 variable would not be placed after the .html in the last line of code.

Any help to get this going is very appreciated!

Regards,
Todd

jdMorgan

7:46 pm on Aug 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Should there be a question mark at the end of the html page on the last line of code?

There should be a question mark at the end of each of those last three rules, unless you want to pass the original query string through to the new URL.

> Also, can you explain to me why the $1 variable would not be placed after the .html in the last line of code.

Because anything that matched into $1 would then be appended after the .html in the new (substitution) URL, and Apache would ignore it there. Since it appears that what is being matched into $1 is the subdirectory path, it would make little sense to put that after the filename.

You have not explained what you intend all of your code to do, or what it actually does, or how that differs from what you want it to do. Therefore, I can only guess.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

CainIV

4:54 am on Aug 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jim thanks for the explanation. All I intend to do is redirect that home.php?cat=255 to a new static page.

The static page is at root of the domain.

Regards,
Todd