Forum Moderators: phranque

Message Too Old, No Replies

301 redirect wont work

         

roham

8:12 am on Nov 25, 2008 (gmt 0)

10+ Year Member



Hi,
I have Old urls with following format and changed them to new format with rewrite mod, for example some of my OLD & NEW URLs and.htaccess file is:
old:
modules.php?name=News
modules.php?name=Forums&file=viewtopic&t=1095

New:
news.html
ftopict-1095.html

.htaccess:
RewriteRule ^news.html modules.php?name=News [L]
RewriteRule ^ftopic(t¦p)-([0-9]*).html modules.php?name=Forums&file=viewtopic&$1=$2 [L]

But promblem is Google index both old & new Urls and make MANY duplicate content,I want to clear old urls from google, I know the solution is 301 redirect but i can't figure the correct syntax in htaccess.for example follownig line wont work and return 200.ok status.
RewriteRule ^modules.php?name=News [mydomain.com...] [R=301,L]

Marcia

8:29 am on Nov 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



roham, your primary issue is probably that there's a query string involved (the ? in the url). Check out some of the threads here on query strings and mod_rewrite [google.com].

Here's one that has a short, clear explanation:

[webmasterworld.com...]

Aside from that, you might also want to read through the Apache documentation for the exact syntax for RewriteCond and RewriteRule, in addition to examining the examples in the referenced threads.

[edited by: Marcia at 8:50 am (utc) on Nov. 25, 2008]

roham

9:56 am on Nov 25, 2008 (gmt 0)

10+ Year Member



Thanks for reply,
I read many threads and finally make following lines but wont work again (for: modules.php?name=News):

RewriteCond %{query_STRING} ^name=([^&]*)$
RewriteRule ^modules\.php\?name=News$ [mydomain.com...] [R=301,L]

(old url: [mydomain.com...]
new url: [mydomain.com...]

[edited by: roham at 10:08 am (utc) on Nov. 25, 2008]

Samizdata

9:57 am on Nov 25, 2008 (gmt 0)

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



The syntax of your rules does not look right to me:

RewriteRule ^news.html modules.php?name=News [L]

should be:

RewriteRule ^news\.html /modules.php?name=News [L]

Hope this helps.

...

roham

10:06 am on Nov 25, 2008 (gmt 0)

10+ Year Member



Hi,
the code:

RewriteRule ^news.html modules.php?name=News [L]

works fine and with "news.html" request prosses "modules.php?name=News" but the problem is reverse state i mean make url:
[mydomain.com...]

to

[mydomain.com...]

and send 301 stat wont work.

phranque

11:08 am on Nov 25, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you have to put your external redirects before your internal rewrites.
within those you should put the most specific rules first and the most general last.

roham

1:40 pm on Nov 25, 2008 (gmt 0)

10+ Year Member



Hi,
after read many threads following lines work great for me, If any better syntax available please help:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /web/modules\.php\?name=News\ HTTP/ [NC]
RewriteRule ^modules\.php$ [mydomain.com...] [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /web/modules.php\?name=Forums&file=viewtopic&t=([0-9]*)\ HTTP/ [NC]
RewriteRule ^modules\.php$ [mydomain.com...] [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /web/modules.php\?name=Forums&file=viewtopic&p=([0-9]*)\ HTTP/ [NC]
RewriteRule ^modules\.php$ [mydomain.com...] [R=301,L]

g1smd

8:55 pm on Nov 25, 2008 (gmt 0)

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



*** RewriteRule ^news.html modules.php?name=News [L] ***

Without escaping the dot, your rule would match news.html and newsZhtml and newsAhtml and news-html and news8html and so on.

g1smd

8:59 pm on Nov 25, 2008 (gmt 0)

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



There's no need to examine THE_REQUEST for the query string information

You could simply look at the QUERY_STRING instead.

Be aware that you also need to cater for the fact that the query parameter might not always be the first or last parameter in the string.

jdMorgan

7:24 pm on Dec 3, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's no need to examine THE_REQUEST for the query string information

You could simply look at the QUERY_STRING instead.

...unless there is also an internal rewrite rule that rewrites client requests for the URL example.com/web/news.html to the filepath /modules\.php\?name=News

If this is the case, then examining THE_REQUEST before doing the redirect is required in order to prevent an 'infinite' redirect/rewrite loop. The logic when examining THE_REQUEST is, "If the query-string URL is being requested directly by the HTTP client (browser or robot), rather than as a result of a previously-invoked internal rewrite, then redirect to the search-engine-friendly static-looking URL. Otherwise, do nothing."

Jim