homepage Welcome to WebmasterWorld Guest from 54.197.215.146
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Multiple issues with rewrite
loops and wrong rewrites
abushahin




msg:4450508
 8:57 pm on May 7, 2012 (gmt 0)

Hi Guys, I have searched the web and here but I haven't found something that would help, probably because i lack the knowledge on what I'm looking for in terms of rewrite expression!

anyway I want to permanently redirect old urls

chapter.php?cid=7&page=1

to this

chapter-7-page-1

now im trying this


Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteCond %{QUERY_STRING} ^cid=(.*)&page=(.*)$
RewriteRule ^chapter\.php$ http://example.com/p/test/chapter-%1-page-%2? [R=301,L]

RewriteRule -([0-9]+)-page-([0-9]+) chapter.php?cid=$1&page=$2


this gives me

http://example.com/s/test/chapter-7&page=1-page-1


now when i remove the - just after page so

RewriteRule ^chapter\.php$ http://example.com/p/test/chapter-%1-page%2? [R=301,L]

I get

http://example.com/s/test/chapter-7-page1

that fails as I have another rule telling it to have the - after page bit.
I suspect its to do with the other rewrites that I have?

I want to be able to redirect chapter.php?cid=1 to chapter-1-page-1 and also chapter.php?cid=1$page=2 to chapter-1-page-2

I would really appreciate some help

I really have no idea whats going on I have looked at jdmorgans

[webmasterworld.com ]
but to no avail!

 

g1smd




msg:4450516
 9:08 pm on May 7, 2012 (gmt 0)

The (.*) patterns are "greedy and ambiguous". Change them.

The rules are certainly in the wrong order, the redirects must be placed before rewrites.

Every rule needs the [L] flag.

You do not need the -d and -f checks at all.

The redirect will need to test THE_REQUEST not QUERY_STRING in order to prevent an infinite redirect-rewrite loop.

abushahin




msg:4451069
 10:08 pm on May 8, 2012 (gmt 0)

Thanks for the reply. I have now changed as you said but if i use THE_REQUEST it doesnt but with QUERY_STRING the rewrite and redirect works but causes infinite looping?

g1smd




msg:4451071
 10:18 pm on May 8, 2012 (gmt 0)

I assume the RewriteCond RegEx pattern for THE_REQUEST is either incomplete or incorrect.

lucy24




msg:4451083
 11:30 pm on May 8, 2012 (gmt 0)

Explanation, if it helps: THE_REQUEST is what the human user-- or their browser-- originally asked for, or a new URL that the browser was redirected to.

So:

{THE_REQUEST} \?
means that they either asked for or were redirected to something containing a query string

{THE_REQUEST} !\?
means that they neither asked for nor were redirected to something containing a query string

{QUERY_STRING} {blahblah}
means only that something, somewhere, added a query string. The user may or may not have any idea that it's happening.

A Redirect (301 or 302) will show up in your logs, exactly as if the human had typed it in the first place. If a Redirect goes around in circles, the user's browser will eventually put up an error message saying "This is going nowhere fast." The browser has to take action because the server doesn't know that the same person was there 2 nanoseconds ago, and 1.5 nanoseconds before that, and 3 nanoseconds before that, and... If you are a certain age, you will like knowing that something in the world has a shorter memory than you.

A Rewrite will not show up in your logs. If a Rewrite goes around in circles, the server will eventually put a stop to it-- usually after ten loops-- and the request will turn into a 500-class error. Only the error code will show up in your regular logs; the error logs may give the whole sordid story.

g1smd




msg:4451086
 11:39 pm on May 8, 2012 (gmt 0)

THE_REQUEST contains the literal request sent by the browser in the HTTP request.

It'll look something like:
GET /thispage.ext?someparam=value HTTP/1.1
or similar.

abushahin




msg:4455670
 8:54 pm on May 20, 2012 (gmt 0)

THE_REQUEST contains the literal request sent by the browser in the HTTP request.

It'll look something like:
GET /thispage.ext?someparam=value HTTP/1.1
or similar.


I've had some time off now I'm back scratching my head about how to do this again!

so

RewriteCond %{THE_REQUEST} \?cid=(.*)&page=(.*)$
RewriteRule ^page\.php$ http://example.com/s/test/page-%1-page-%2? [R=301,L]
RewriteRule -([0-9]+)-page-([0-9]+) page.php?cid=$1&page=$2 [L]


is giving me

example.com/page-7-page-1 HTTP/1.0

This is working but with that in the url it doesnt change and all the urls are appended to this.

I know that (.*) is bad for this but if i use ([0-9]+) it doesnt work at all.

Ive searched high and low but cannot come to somewhere that has a similar problem to mine

any sort of help much much appreciated!

g1smd




msg:4455676
 9:07 pm on May 20, 2012 (gmt 0)

This should work.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /page\.php\?cid=([0-9]+)&page=([0-9]+)\ HTTP/
RewriteRule ^page\.php$ http://example.com/chapter-%1-page-%2? [R=301,L]

RewriteRule ^chapter-([0-9]+)-page-([0-9]+)$ /page.php?cid=$1&page=$2 [L]


In your original code you have the word "page" where the word "chapter" should be.

Adjust the code as necessary to fit your test server scenario. It appears you are testing in a folder. This is usually a bad idea. You should test in a subdomain so that minimal changes need to be made to the code when moving it to the live server. Additionally code in the htaccess file in a folder made be badly influenced by code in the htaccess files in higher folder levels. This is usually avoided by using a subdomain for testing.

abushahin




msg:4455695
 10:10 pm on May 20, 2012 (gmt 0)

Thanks for the quick response! I really appreciate it!

For some reason it doesnt work and on top of that it's now not finding the css.

heres all my code:

Options -MultiViews
RewriteEngine On
###RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
####RewriteCond %{THE_REQUEST} ^cid=([0-9]+)&page=([0-9]+)$
####RewriteRule ^chapter\.php$ http://example.com/s/test/chapter-%1-page-%2? [R=301,L]
####RewriteRule -([0-9]+)-page-([0-9]+) chapter.php?cid=$1&page=$2 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /chapter\.php\?cid=([0-9]+)&page=([0-9]+)\ HTTP/
RewriteRule ^chapter\.php$ http://example.com/s/test/chapter-%1-page-%2? [R=301,L]
RewriteRule ^chapter-([0-9]+)-page-([0-9]+)$ /chapter.php?cid=$1&page=$2 [L]
RewriteRule -([0-9]+)-chapter-([0-9]+) bookswithimages.php?id=$1&cid=$2 [L]
RewriteRule -words-([0-9]+) gatesopen.php?id=$1 [L]
RewriteRule -book-([0-9]+)-letter-(.*) narrator.php?bid=$1&let=$2 [L]
RewriteRule -(.*)-([0-9]+)-letter-(.*) bynarrator.php?n=$1&bid=$2&let=$3 [L]
RewriteRule p-([0-9]+) qwords.php?page=$1 [L]
RewriteRule p-([0-9]+) 101words.php?page=$1 [L]


I'm not really sure but I think some are clashing with eachother.

Another question is is it really necessary to have 301 for canonicalisation or can I just use rel=canonical and that may suffice? then I may just decide to use that?

Once again thanks for the prompt and excellent support!

g1smd




msg:4455707
 10:34 pm on May 20, 2012 (gmt 0)

htaccess is not at all responsibe for "finding the CSS".

If you use a relative link to the CSS file, the browser asks for the CSS file based on the current URL as seen in the browser URL bar. That would be a huge error when using rewritten URLs. Use a link that begins with a leading slash and includes the full path to the file.

Your rules are possibly in the wrong order. The rule with (.*) in the pattern potentially eats requests that it should not.

The rest of your rule patterns have no start anchoring. They are therefore ambiguous and match requests they should not do.

Many of the rules will never run at all because an earlier rule already matched the request.

A blank line after each RewriteRule makes for code that is easier to read.

[edited by: g1smd at 10:47 pm (utc) on May 20, 2012]

abushahin




msg:4455712
 10:46 pm on May 20, 2012 (gmt 0)

Hey thanks for the quick reply!

I wanted to know if its enough to have a canonical tag on the pages without the 301 redirects, as i cant find any info from search engine providers.

as for my code. ive put in the order or removed .*

Options -MultiViews

RewriteEngine On

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /chapter\.php\?cid=([0-9]+)&page=([0-9]+)\ HTTP/

RewriteRule ^chapter\.php$ http://example.com/s/test/chapter-%1-page-%2? [R=301,L]

RewriteRule ^chapter-([0-9]+)-page-([0-9]+)$ /chapter.php?cid=$1&page=$2 [L]

RewriteRule -([0-9]+)-chapter-([0-9]+) bookswithimages.php?id=$1&cid=$2 [L]

RewriteRule -words-([0-9]+) gatesopen.php?id=$1 [L]

RewriteRule -book-([0-9]+)-letter-(.*) narrator.php?bid=$1&let=$2 [L]

RewriteRule p-([0-9]+) qwords.php?page=$1 [L]

RewriteRule p-([0-9]+) 101words.php?page=$1 [L]

RewriteRule -(.*)-([0-9]+)-letter-(.*) bynarrator.php?n=$1&bid=$2&let=$3 [L]

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^(.*)$ $1.php


Any help very much appreciated!

g1smd




msg:4455713
 10:54 pm on May 20, 2012 (gmt 0)

Many requests will match this pattern:
-([0-9]+)-chapter-([0-9]+)

The first one matches some requests and the identical pattern on the next rule is never used:
p-([0-9]+)

Absolutely not!
-(.*)-([0-9]+)-letter-(.*)
The (.*) is ambiguous. The lack of anchoring is ambiguous.

The entire ruleset is a mess.

Start anchor EVERY rule.
End Anchor every rule.
Change the (.*) pattern to something less ambiguous unless it is at the END of the rule pattern.
Alter the rule patterns so that each are unique. Multiple patterns should not apply as the first matching rule will "eat" all the requests.

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