Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite troubles

         

chemoul

6:08 am on Aug 16, 2006 (gmt 0)

10+ Year Member



I've come up with the following mod rewrite rules:

RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*)/(.*)$ index.php?page=$1&subpage=$2
RewriteRule ^/(.*)$ index.php?page=$1

So, for [domain.com...]
it should translate to [domain.com...]
and for [domain.com...]
it should translate to [domain.com...]

The problem is that redirecting to subarticles doesn't work. Also I think I would have to exclude index.php somehow, so that no rewrite rules are applied if someone just types in [domain.com...] And it shouldn't matter whether a / is inserted at the end of the url, so [domain.com...] and [domain.com...] as well as [domain.com...] and [domain.com...] should be accepted.

Any ideas?

Thanks.
Steve

jdMorgan

1:18 pm on Aug 16, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're falling victim to the ambiguous and 'greedy' ".*" pattern, and Apache mod_dir, most likely.

I'd suggest a more-specific pattern -- 'look for anything up to the next slash, if there is one'.


RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1&subpage=$2 [L]
#
RewriteCond $1 !index\.php$
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]

In addition to not failing due to mod_dir interactions, the patterns in this code can also be processed faster than those in the original, because the pattern is more specific and the boudaries between parameters are unambiguous.

Jim

chemoul

11:34 am on Aug 18, 2006 (gmt 0)

10+ Year Member



Thank you for your help.
Only problem is that it now only shows me the contents of the directories, which means somewhere along the line the mod rewrite doesn't give the rewritten string back to the index.php and doesn't call the index.php.

This happens when trying to access a URL like http://www.example.com/article1

Any ideas?

Thanks.

This is the mod_rewrite log, if it helps (site is http://localhost/textcms/phitetest, PT_003_Basic_Concepts is the string that should be passed as index.php?page=$1)


[rid#1184108/initial] (2) init rewrite engine with requested uri /textcms/phitetest/PT_003_Basic_Concepts
127.0.0.1 - - [18/Aug/2006:21:43:21 +1000] [localhost/sid#37bed0][rid#1184108/initial] (1) pass through /textcms/phitetest/PT_003_Basic_Concepts
127.0.0.1 - - [18/Aug/2006:21:43:21 +1000] [localhost/sid#37bed0][rid#58d1cc8/initial] (2) init rewrite engine with requested uri /textcms/phitetest/PT_003_Basic_Concepts/
127.0.0.1 - - [18/Aug/2006:21:43:21 +1000] [localhost/sid#37bed0][rid#58d1cc8/initial] (1) pass through /textcms/phitetest/PT_003_Basic_Concepts/
<snip>
127.0.0.1 - - [18/Aug/2006:21:43:21 +1000] [localhost/sid#37bed0][rid#1182080/subreq] (2) init rewrite engine with requested uri /textcms/phitetest/PT_003_Basic_Concepts/index.phtml
127.0.0.1 - - [18/Aug/2006:21:43:21 +1000] [localhost/sid#37bed0][rid#1182080/subreq] (1) pass through /textcms/phitetest/PT_003_Basic_Concepts/index.phtml
127.0.0.1 - - [18/Aug/2006:21:43:21 +1000] [localhost/sid#37bed0][rid#1182080/subreq] (2) init rewrite engine with requested uri /textcms/phitetest/PT_003_Basic_Concepts/HEADER.html
127.0.0.1 - - [18/Aug/2006:21:43:21 +1000] [localhost/sid#37bed0][rid#1182080/subreq] (1) pass through /textcms/phitetest/PT_003_Basic_Concepts/HEADER.html

[edited by: jdMorgan at 2:34 pm (utc) on Aug. 18, 2006]
[edit reason]
[1][edit reason] Example.com, de-linked, snipped [/edit]
[/edit][/1]

jdMorgan

2:38 pm on Aug 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, all requests result in a pass-through, which means that the rule pattern is not matching.

I also see no indication of 'per-dir' rewriting in the log; If your code is in httpd.conf or conf.d, as opposed to being in .htaccess, then all patterns must start with "/", so you may need to modify what I posted above:


RewriteRule [b]^/[/b]([^/]+)/([^/]+)/?$ index.php?page=$1&subpage=$2 [L]
#
RewriteCond $1!index\.php$
RewriteRule [b]^/[/b]([^/]+)/?$ index.php?page=$1 [L]

Jim

chemoul

8:52 pm on Aug 18, 2006 (gmt 0)

10+ Year Member



thank you for your reply Jim.
the rewrite rules are in a .htaccess file in the directory [localhost...] all links of the php script are absolute, i.e. [localhost...]

I really appreciate your help.

Chemoul

jdMorgan

9:37 pm on Aug 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since your example URL and the local URL-paths in the RewriteLog don't agree, I can't really tell you anything. However, you must be sure that your DocumentRoot directive and any Alias directives in httpd.conf comport with the RewriteBase in .htaccess.

Check your error log as well as the rewritelog What you are looking for is an unexpected path element that is causing the rule not to match.

Also, you might want to test with a greatly-simplified RewriteRule -- first a rule that rewrites or redirects any request for any resource in that subdirectory to something obivious -- say, a redirect to Google. Then, once you get the request-URL matching working, you can use a more complex pattern. Get that working, then change the redirect path (substitution URL) to an internal page, then add parameter-passing, and finally, finish up by changing the redirect to an internal rewrite. Redirects have the advantage that since they update your browser address bar, you can watch them working (or not working).

By watching your rewrite and error logs during this process, you should be able to figure out what the problem is.

Jim

chemoul

2:16 am on Aug 19, 2006 (gmt 0)

10+ Year Member



I think I've figured it out, thanks.
I had to add
RewriteEngine on
Options +FollowSymlinks
and now it's working.

One other question though, that keeps coming up whenever I try to do something with mod rewrite: relative and absolute links.
So far, whenever I let my php script write a relative link, the rewritten part gets added to the actual url, which then forms an incorrect url.
For example I click on the link [domain.com...]
the page loads (really being [domain.com...]
If I then click on the same link again it tries to go to [domain.com...]
Apart from defining absolute urls in php (i.e. [domain.com...] instead of article1/) is there another way?

Thank you for your help.

chemoul

2:42 am on Aug 19, 2006 (gmt 0)

10+ Year Member



Sorry but there is one more problem. When clicking on a link, such as [domain.com...] or [domain.com...] mod rewrite redirects to the correct page, but the url in the browser window is something like:
[domain.com...] or [domain.com...]

I am sure that this query string does not get added in the php script.
Attached is the rewrite log for calling an article called PT_001_Quick_Start.

Thank you so much.


127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#5845398/initial] (2) init rewrite engine with requested uri /textcms/phitetest/PT_001_Quick_Start
127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#5845398/initial] (1) pass through /textcms/phitetest/PT_001_Quick_Start
127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#5845398/initial] (2) [per-dir C:/webserver/htdocs/textcms/phitetest/] rewrite PT_001_Quick_Start -> index.php?page=PT_001_Quick_Start
127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#5845398/initial] (2) [per-dir C:/webserver/htdocs/textcms/phitetest/] strip document_root prefix: C:/webserver/htdocs/textcms/phitetest/index.php -> /textcms/phitetest/index.php
127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#5845398/initial] (1) [per-dir C:/webserver/htdocs/textcms/phitetest/] internal redirect with /textcms/phitetest/index.php [INTERNAL REDIRECT]
127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#5840388/initial] (2) init rewrite engine with requested uri /textcms/phitetest/PT_001_Quick_Start/
127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#5840388/initial] (1) pass through /textcms/phitetest/PT_001_Quick_Start/
127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#5840388/initial] (2) [per-dir C:/webserver/htdocs/textcms/phitetest/] rewrite PT_001_Quick_Start/ -> index.php?page=PT_001_Quick_Start
127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#5840388/initial] (2) [per-dir C:/webserver/htdocs/textcms/phitetest/] strip document_root prefix: C:/webserver/htdocs/textcms/phitetest/index.php -> /textcms/phitetest/index.php
127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#5840388/initial] (1) [per-dir C:/webserver/htdocs/textcms/phitetest/] internal redirect with /textcms/phitetest/index.php [INTERNAL REDIRECT]
127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#583d5f0/initial/redir#1] (2) init rewrite engine with requested uri /textcms/phitetest/index.php
127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#583d5f0/initial/redir#1] (1) pass through /textcms/phitetest/index.php
127.0.0.1 - - [19/Aug/2006:12:35:27 +1000] [localhost/sid#37c258][rid#583d5f0/initial/redir#1] (1) [per-dir C:/webserver/htdocs/textcms/phitetest/] pass through C:/webserver/htdocs/textcms/phitetest/index.php

jdMorgan

3:43 am on Aug 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post your current code if you make *any* changes. Otherwise we have no idea why your code does this or that, because we don't know what your code looks like... :(

If you are using the code in the first five posts above, or anything like it, then that code will not update your browser address bar, because internal rewrites do not communicate with the browser. The server simply supplies the requested resource from the rewritten URL to the browser, and the browser remains completely unaware of the rewrite. Only external redirects can tell the browser to re-request content from a new URL, and that is what changes your address bar.

Also, it is the browser thar resolves links on pages -- the server has nothing to do with it. It is the browser that takes an on-page link like <a href="foo.html"> and canonicalizes it by removing the current "page" URL-path from the URL in its address bar and adding "foo.html" to the result, yielding the canonical URL.

Simliarly, if it sees <a href="/foo.html">, it will remove the entire local URL-path from the address bar URL, leaving only the current domain, and then add "/foo.html" to that to get a canonical URL.

If you use <a href="http://example.com/bar.html">, then the browser does not use the current URL at all, it simply takes the entire URL as specified in the <a href> tag.

All of this is done by the browser, so the only way the browser could 'see' your dynamic URLs and show them in the address bar is if your mod_rewrite code, mod_alias code, or php script is generating an external redirect. I'd suggest you look for that first, unless you modified your code to generate redirects as suggested for a test, and have not changed it back.

Jim

chemoul

6:47 am on Aug 19, 2006 (gmt 0)

10+ Year Member



Hi Jim,

this is the code I use:


RewriteEngine on
Options +FollowSymlinks
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?page=$1&subpage=$2 [L]
#
RewriteCond $1!index\.php$
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]

It is correct that the URL in the browser bar doesn't update itself if I type it in there. I was referring to clicking on a link on the page, sorry if I was unclear.
So clicking on a link on the page, ie [domain.com...] loads the correct page, but the url in the browser location bar reads [domain.com...] when the page is loaded. As I said I checked the php script and there is nothing in there that would add or redirect to anything with "?page" in it.
I just can't see where and why it would mod_rewrite with?page in it?