Forum Moderators: phranque

Message Too Old, No Replies

Internal Rewrite External SEO Redirects help.

ReWrites work but Redirects cause error 500

         

cristo

12:15 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



Requirement:

A) To change a small number of dynamic urls to more 'friendly' urls.

e.g.change
http://www.example.com/index2.php?content_id7
to
http://www.example.com/faq.htm

This is achieved by:
1. Changing internal links to new faq.htm
2. Creating the following internal rewrite line in .htaccess:

 RewriteRule faq\.htm$ index2.php?content_id=7 [L]

Which works very well for my needs.
(Note: External back-links are currently are not an issue with this site - they all point to http://www.example.com)

B) To update SE indexes by removing old dynamic urls from index and pointing to new url permanently:
e.g.

...index2.php?content_id7 with ...new faq.htm 

This is (theoretically) to be achieved by:
Create an External redirect using .htaccess: I have tried this many different ways but it always gives an error 500. Even trying a simple one like this does not work:

e.g. RedirectMatch 301 login.php login.htm

So my .htaccess might look like this:

RewriteEngine on
RewriteBase /

# internal redirect - works fine
RewriteRule login\.htm$ login.php [L]

#External SEO redirect - returns error 500
RedirectMatch 301 login.php login.htm
this also does not work:
#RewriteRule index2\.php?content_id=7$ faq.htm [R=301, L]

I have tried using redirect, redirectmatch and rewrite to achieve this but it won't play.

Any chance of some suggestions as what I am doing wrong?!..you'd save my bacon as I am delaying putting this site live due to this!

[edited by: jdMorgan at 12:29 pm (utc) on Oct. 25, 2007]
[edit reason] example.com [/edit]

jdMorgan

12:28 pm on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> RedirectMatch 301 login.php login.htm

In order to determine why this causes a server error, it's necessary to know what the relationship between the two URL-paths is. For example, are both defined as DirectoryIndex pages? Is login.htm later rewritten to login.php, creating an 'infinite' rewrite/redirect loop?

Also, it would be most helpful to know why the server is generating a 500-Server Error -- This information is available in your server error log.

It's not trivial to do what you're probably trying to do and avoid looping, even with a well-defined scope. For example, in order to rewrite from "/" to "index.php" and to redirect client requests from index.php to "/" you'd need:


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

The RewriteCond in the second rule prevents redirection when index.php is requested as a result of the first rule, while allowing redirection if index.php is directly requested by the client (e.g browser or robot).

Jim

cristo

3:20 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



So, based on what you described and my example I have transcribed it to the following:

RewriteRule login\.htm$ login.php [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /login\.php\ HTTP/
RewriteRule ^login\.php$ http://example.com/login.htm [R=301,L]

Which works!
I had a hunch looping was the issue but couldn't see the wood for the trees. I was close at times but using the HTTP part of the request is an eye opener. Thank you so much.

...and thank you for the forum year after year - excellent stuff.

cristo

4:08 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



Hmmm...struggling again. I have checked your regex tutorial but can't get it.

I got the example (above) working but seem to be having trouble with the dynamic url - Is there something I need to escape that I'm missing?

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?content_id=1\ HTTP/
RewriteRule ^index\.php\?content_id=1$ http://example.com/home.htm [R=301,L]

I have tried without escaping the '?' and/or with escape for t he underscore but no joy - it does not pick it up.

Any thoughts?

I am not totally clear on this part of the expression you provided:
^[A-Z]{3,9}\
Is it something do with this not picking up the '?' and the '_'? Would you mind explaining this part for clarity.
Thank you again.

jdMorgan

4:24 pm on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The query string is not part of the URL, but rather, data attached to the URL to be passed to the resource at that URL. Therefore, it is not accessible to RewriteRule, but only as %{QUERY_STRING} or as part of %{THE_REQUEST}:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?content_id=1\ HTTP/
RewriteRule ^index\.[b]php$[/b] http://example.com/home.htm [R=301,L]

THE_REQUEST is the entire request header received from the client, for example:
GET /index.php?content_id=1 HTTP/1.1

so the "[A-Z]{3,9}" subpattern is intended to match all valid HTTP methods, e.g. GET, HEAD, POST, PROPFIND etc.

Jim

cristo

5:10 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



this works but:

# index.php?content_id=1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?content_id=1\ HTTP/
RewriteRule ^index\.php$ http://example.com/home.htm [R=301,L]

SHOULD 'replace' example.com/index.php?content_id=1
with example.com/home.htm

BUT in fact it 'replaces and appends' with following result:
example.com/home.htm/index.php?content_id=1

Why is it appending the original index.php?content_id=1?

jdMorgan

5:12 pm on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops... Stick a "?" on the end of the substitution URL:

...
RewriteRule ^index\.php$ http://example.com/home.[b]htm?[/b] [R=301,L]

Jim

cristo

10:30 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



amazing. Yes that fixed it. I am very happy now Thank you.

How do you know this? Is there a good resource you can recommend to lookup things like terminating the replacement string?..because I can't find it!

Can I push my luck and ask one more thing?!

I now have another similar, but different issue:

say I want to redirect (301) example.com/search.php with example.com/search.htm

BUT

not to redirect search.php if it has additional elements attached:
e.g. search.php?action=results&page=2

Would I need to write an additional rewrite condition to say
e.g. but NOT if search.php?action=results&page=2 if so can show me a not expression - I know its possible to use the <!> but I can't find the syntax for this.

....Or is there a way to say redirect if = search.php and NOT if <> search.php

Once again thank you for your excellent advice and instruction.

jdMorgan

11:36 pm on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Examples of rewrites/redirects based on the query string value are pretty common here in the forum -- try a search adn you should be able to turn up several.

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

g1smd

1:07 pm on Oct 26, 2007 (gmt 0)

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



Test %{QUERY_STRING} to see if it is empty, or if it does (or does NOT) match a specific pattern.