homepage Welcome to WebmasterWorld Guest from 54.211.80.155
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Rewrite rule not quite working correctly
My rewrite rule is sending things to both options istead of just one
GF_Diablos




msg:3822000
 4:03 pm on Jan 8, 2009 (gmt 0)

I have a small htaccess problem, wonder if anyone can help.

Basically I have a CMS on the site (built by a programmer especially for us) than writes to two categories, news and articles. I put a redirect in but it is creating a redirect for everything twice, one for new and one for article, leading me to duplicate content.

Here is the code i'm using.


RewriteRule keyword-advice-article-(.*)\.php article.php?id=$1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /article\.php\?id=([0-9]+)\ HTTP/
RewriteRule ^article\.php$ [url]http://www.example.co.uk/keyword-advice-article-%1.php?[/url] [R=301,L]

RewriteRule keyword-news-(.*)\.php news.php?id=$1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news\.php\?id=([0-9]+)\ HTTP/
RewriteRule ^news\.php$ [url]http://www.example.co.uk/keyword-news-%1.php?[/url] [R=301,L]

The aim is to rerwrite and redirect article.php?=100 to advice-article-100.php and news entries to keyword-news-101.php obviously the number being variable.

To cut the story short this code is making it so both those urls exist all the time.

How do I make it so that news rule only rewrites news stories and the article rule only rewrites article stories.

 

jdMorgan




msg:3822065
 5:05 pm on Jan 8, 2009 (gmt 0)

Other than the missing pattern anchors and [L] flags, I don't see anything that corresponds to "redirecting twice" or "allowing both to exist" here...

For clarity, I would re-order the rules, though:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /article\.php\?id=([0-9]+)\ HTTP/
RewriteRule ^article\.php$ http://www.example.co.uk/keyword-advice-article-%1.php? [R=301,L]
RewriteRule ^keyword-advice-article-(.*)\.php article.php?id=$1 [L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news\.php\?id=([0-9]+)\ HTTP/
RewriteRule ^news\.php$ http://www.example.co.uk/keyword-news-%1.php? [R=301,L]
RewriteRule ^keyword-news-(.*)\.php news.php?id=$1 [L]

It is possible the that CMS script itself is generating a redirect, in which case this code and the CMS will conflict and set up an 'infinite' rewrite/redirect loop. If you can better describe what is happening with a specific (single) URL example, and the resulting HTTP redirection headers, that would be good.

Jim

GF_Diablos




msg:3822559
 8:09 am on Jan 9, 2009 (gmt 0)

Sorry will try to give a more clear explanation.

I have 2 categories for content, news and articles.

Say I post a new article using the system in the articles category it would usually output to www.example.co.uk/aticle.php?=100. With the rewrite rule it should go to www.example.co.uk/keyword-advice-article-100.php which it does but it also appear at the other rewrite rule www.example.co.uk/keyword-news-100.php.

This problem happens both ways so news stories also appear under article urls. I don't think it is the CMS because it doesn't do it if I remove the rules.

g1smd




msg:3822668
 1:03 pm on Jan 9, 2009 (gmt 0)

Yes it is the URL in the links in the page that define the URLs.

In this case you have two rewrites and both of the URL formats that they accept as inputs, result in the same piece of content being pulled from the server.

That's a Duplicate Content issue that you need to solve by modifying the scripts running the website so that there is only one URL being used to describe each piece of content.

[edited by: g1smd at 1:08 pm (utc) on Jan. 9, 2009]

g1smd




msg:3822669
 1:07 pm on Jan 9, 2009 (gmt 0)

*** it also appear at the other rewrite ***

Understand that a rewrite does not "create" URLs.

That is exactly backwards.

A rewrite takes a request for URL example.com/aaaaa and "translates" that request so that the content is silently pulled from /zzzzz inside your server, without revealing what that location actually is.

In this case you have another rewrite, one that accepts a request for URL example.com/bbbbb but it also pulls the content from the exact same /zzzzz place inside your server.

That's a problem. You have Duplicate Content.

Your pages refer to both example.com/aaaaa and to example.com/bbbbb, so both URLs "work". If you only refer to one URL format on your pages, there will only be one URL format for that content.

I would drop both advice-article and news from both types of URLs.

GF_Diablos




msg:3824586
 3:40 pm on Jan 12, 2009 (gmt 0)

Sorry for slow reply, my computer killed itself this weekend.

Thanks for the advice guys, I know it doesn't create a page I just don't get why it would only be broken when the rewrites are active.

Do you think the best option is simply to drop one rewrite rule and have just one url and settle with it. I can easily pick up what pages are going to old source and redirect them.

I would usually have the shorter url g1smd but its already in use on the server :)

jdMorgan




msg:3824620
 4:27 pm on Jan 12, 2009 (gmt 0)

Say I post a new article using the system in the articles category it would usually output to www.example.co.uk/aticle.php?=100.

That is the problem. For each 'type' of URL --"keyword-news" and "keyword-advice-article"-- you have only implemented two-thirds of the solution. To avoid problems with slow "visitor experience" and search engines, you must modify the script(s) that produce your pages so that they output the "SEO friendly" URL as the link-URL appearing on your pages. You cannot rely on the two redirect rules to "fix" the problem; Those rules can only help speed up search engines' removal of listings of the old "unfriendly" URLs, and only if you link to the correct URLs on your HTML pages.

If those redirects are not working properly, that is a second problem. Test the redirects by requesting the "unfriendly" URLs with the "Live HTTP Headers" add-on for Firefox and Mozilla browsers enabled. If you do not see a 301 redirect straight to the correct "friendly" URL, then report back here with what you find.

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved