Forum Moderators: phranque

Message Too Old, No Replies

Rewrite to different path

         

dari

3:54 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



I have some phpBB2 forums running at [test.example.net...] and want to rewrite the URLs such that they look like:
[test.example.net...]

instead of

[test.example.net...]

I've gotten rewrites to make it successfully rewrite to

[test.example.net...]

But, I want to strip out the "phpBB2" portion of the path to match existing pages that these will be replacing at the primary site (I don't want users to get lots of 404s when visiting bookmarked sites).

The .htaccess file in the phpBB2/ directory now looks like this:

RewriteEngine On

RewriteRule ^forums.html index.php [L]
RewriteRule ^viewforum([0-9]*)-([0-9]*).html viewforum.php?f=$1&start=$2
RewriteRule ^forum([0-9]*).html viewforum.php?f=$1&mark=topic
RewriteRule ^forum([0-9]*).html viewforum.php?f=$1

RewriteRule ^ptopic([0-9]*).html viewtopic.php?t=$1&view=previous
RewriteRule ^ntopic([0-9]*).html viewtopic.php?t=$1&view=next
RewriteRule ^ftopic([0-9]*)-([0-9]*)-([a-zA-Z]*)-([0-9]*).html viewtopic.php?t=$1&postdays=$2&postorder=$3&start=$4
RewriteRule ^ftopic([0-9]*)-([0-9]*)-([a-zA-Z]*)-highlight([0-9A-Za-z]*)-([0-9]*).html viewtopic.php?t=$1&postdays=$2&postorder=$3&highlight=$4&start=$5
RewriteRule ^ftopic([0-9]*)-([0-9]*).html viewtopic.php?t=$1&start=$2
RewriteRule ^ftopic([0-9]*)-highlight([0-9a-zA-Z]*).html viewtopic.php?t=$1&highlight=$2
RewriteRule ^ftopic([0-9]*).html viewtopic.php?t=$1
RewriteRule ^ftopic([0-9]*).html viewtopic.php?t=$1&start=$2&postdays=$3&postorder=$4&highlight=$5
RewriteRule ^post([0-9]*).html viewtopic.php?p=$1
RewriteRule ^print([0-9]*)-([0-9]*).html printview.php?t=$1&start=$2
RewriteRule ^viewprofile([0-9]*).html
profile.php?mode=viewprofile&u=$1

Again, this rewrites to
[test.example.net...]
instead of to
[test.example.net...]

What do I need to add to the .htaccess to remove the phpBB2? Do I need to move it into the parent directory?

Thanks in advance.

-Dariush Molavi

[edited by: jdMorgan at 4:32 pm (utc) on Dec. 1, 2004]
[edit reason] Removed specifics per TOS [/edit]

jdMorgan

4:23 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Dariush,

Welcome to WebmasterWorld!

Again, this rewrites to
[test.example.net...]
instead of to
[test.example.net...]

One thing that will make the solution more obvious is to correct the wording of that concept. Your rules rewrite from http://test.example.net/phpBB2/forum7.html to a call to your viewforum.php script.

mod_rewrite works after an HTTP request is received by your server, but before any content is served or any scripts are invoked. Therefore, mod_rewrite modifies incoming URLs, and not the outgoing URLs seen on your pages in a browser.

Therefore, as long as "phpBB2" is not needed to make each forum's URL unique, you can remove the "phpBB2" from the on-page URLs generated by your scripts, and then use mod_rewrite to put "phpBB2" back into the path needed to call your scripts. As you surmised, the RewriteRules will have to be moved to the directory above phpBB2 in order for this to work.

I also note that you have two sets of duplicated patterns in your rules, and the second rule in each pair will never be invoked.


RewriteRule ^forum([0-9]*).html viewforum.php?f=$1&mark=topic
RewriteRule ^forum([0-9]*).html viewforum.php?f=$1
-and-

RewriteRule ^ftopic([0-9]*).html viewtopic.php?t=$1
RewriteRule ^ftopic([0-9]*).html viewtopic.php?t=$1&start=$2&postdays=$3&postorder=$4&highlight=$5

In both cases, the second rule will never be invoked, because the first one, if invoked, will change the URL before the second rule is tested.

Also, if you always expect at least one number in the requested URL, you can use [0-9]+ instead of [0-9]* to achieve a slight speed increase.

Jim

dari

4:29 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



Therefore, as long as "phpBB2" is not needed to make each forum's URL unique, you can remove the "phpBB2" from the on-page URLs generated by your scripts, and then use mod_rewrite to put "phpBB2" back into the path needed to call your scripts. As you surmised, the RewriteRules will have to be moved to the directory above phpBB2 in order for this to work.

So, if I move this to the parent directory, would I have to add a RewriteBase to the .htaccess or just insert the "phpBB2/" portions to the right-hand portions of the rewrite rule?

EDIT: I moved it, added "phpBB2/" to the right-hand portions, and get this as a result:
[test.example.net...]

The page redirects just fine, but the stylesheets and images are broken. Do I need to add additional rewrites to handle images and css stylesheets, or do I need to change the actual template/php files that generate the page?

[edited by: dari at 4:52 pm (utc) on Dec. 1, 2004]

jdMorgan

4:34 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> insert the "phpBB2/" portions to the right-hand portions of the rewrite rule.

Yes. RewriteBase is for compensating for aliased "user" paths, and is not needed here.

Jim

dari

4:42 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



hehe..guess we crossed wires here...check my edit in the post above. :)

thanks for all your help so far :)

more info on the error, here's the apache log entry for one such broken image:

a.b.c.d - - [01/Dec/2004:11:53:42 -0500] "GET /templates/nukedgallery/images/folder_hot.gif HTTP/1.1" 404 328 "http://test.example.net/forum5.html" "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0"

The attempt is being made at trying to pull it from /templates/blah/blah instead of /phpBB2/templates/blah/blah ... is there a rewrite rule I can use to fix this?

jdMorgan

5:11 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Do I need to add additional rewrites to handle images and css stylesheets, or do I need to change the actual template/php files that generate the page?

You can do it either way, whichever you find easier/better for your long-term plans.

Jim

dari

5:14 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



what would the rewrite way be? all methods i've tried thus far have failed.

one potential sticking point:
both the phpBB2/ directory and it's parent have a directory called images/ within them.

dari

1:23 pm on Dec 2, 2004 (gmt 0)

10+ Year Member



<bump>
any ideas? anyone? bueller? bueller?

dari

4:50 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



i've tried all manners of rewrites and i cannot get this to work. the redirection works, but the images and CSS sheets are not pulled properly, resulting in contextually correct pages, but broken images and horrendous layout.

if anyone has any suggestions as to how to get the rewrite rule to include images and css, PLEASE help.

jdMorgan

5:01 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> one potential sticking point: both the phpBB2/ directory and it's parent have a directory called images/ within them.

So you'll need to change the links on your php pages to access the images in the correct location. Since you've got two conflicting /images directories, mod_rewrite can't help here. An alternative would be to rename one of the /images directories and change the links that refer to it.

Jim

dari

5:19 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



i was afraid of that...
c'est la vie..time to make my 404 handler a little more intelligent.. (i did this already as a backup, so it won't be any additional work)

jdMorgan

6:16 pm on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't use a 404 handler to to redirects! You will seriously confuse search engines, and may possibly destroy your rankings by returning a 404 when a 30x response is needed. Use a 404 handler for handling truly-unexpected errors only.

Sit back from this problem for a few days and think about ways to re-architect your site's directory structure. Among other things, you could move your forum to a subdomain, and thereby eleiminate all conflicts with the non-forum URLs.

Jim

dari

6:32 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Among other things, you could move your forum to a subdomain, and thereby eleiminate all conflicts with the non-forum URLs.

PERFECT! I'll just make a forums.test.net subdomain...too much thinking clouds the mind at times. thanks for everything :)

dari

3:41 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



ok, so the subdomain idea didn't work, mainly because it forced users to log into the site twice, once at the main site, again at the forums. moving it out of the subdomain and back into the primary site solved that problem, but i'm still struggling with the rewrite issue for the CSS sheets, etc. In the phpBB2 subdirectory, i've put a .htaccess file with this:

RewriteRule ^/$ phpBB2/ [L]

to rewrite all URLs to the phpBB2 subdirectory, and thus pull the images and CSS sheets. This isn't working, though.

So, I tried:

RewriteRule ^.*$ /phpBB2/$1 [L]

No dice.

The site directory structure is:
/foo/bar ----> main site
/foo/bar/phpBB2 -----> forum site

Should the htaccess file with the phpBB2 specific rules still be in /foo/bar (ie, the CSS is in /foo/bar/phpBB2/templates/foo/ , so should I add a .haccess rule in /foo/bar to the effect of:

RewriteRule /templates/.*$ /phpBB2/templates/$1 [L]

?

EDIT: here is some more info:
in my /foo/bar, i've got an htaccess rule like this:
RewriteRule ^post(p¦t)([1-9][0-9]*).* phpBB2/viewtopic.php?$1=$2 [L]

which would redirect something like postp123.html to phpBB2/viewtopic.php?p=123

actually, now that i'm thinking about it, perhaps something like

RewriteRule ^post(p¦t)([1-9][0-9]*).* phpBB2/post$1$2.html [L]

and then in the phpBB2 directory have the rule like:

RewriteRule ^post(p¦t)([1-9][0-9]*).* viewtopic.php?$1=$2 [L]

it's two rewrites, but it's the best i can think of. if Jim or anyone else has a more efficient way, please let me know.

jdMorgan

4:06 pm on Dec 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This isn't working, though.

So, I tried:

RewriteRule ^.*$ /phpBB2/$1 [L]

No dice.

Please explain what you mean. "This isn't working" is not at all helpful to someone who is not looking over your shoulder. It is critical that you explain what does not happen as well as what does happen, otherwise, we can only guess. Give the limited amount of time that members can devote to reading and answering threads here, the most specific and detailed questions get answered, and the ones that don't contain sufficient information get lost.

The only guess I have is that a rule like that is likely to loop, because the output URL will match the input requirements, and on many servers, mod_rewrite in a directory context (.htaccess) is recursive.

To avoid a loop, you need to explicitly disallow it. Also, your back-reference $1 is undefined. Back-references are created by the use of parentheses around the parts of the pattern that you wish to back-reference:


RewriteCond %{REQUEST_URI} !^/phpBB2/
RewriteRule ^(.*)$ /phpBB2/$1 [L]

The rules need to go into the directory at the level above all those which you want them to affect, so in this case, the directory above /phpBB2.

Jim

dari

4:17 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



Touche :)

Alrighty, here's what happens with the following rewrites:
RewriteRule ^/$ phpBB2/ [L]

RewriteRule ^.*$ /phpBB2/$1 [L]

Both give me pages with the proper content, but broken images and no CSS applied (a viewing of the source shows that the CSS in the source is at "templates/foo/foo.css", when in reality, it's at "phpBB2/templates/foo/foo.css", but since the URL being viewed is "www.test.net/post123.html", it's not getting the proper path to the CSS and images.).

I've tried the rules in my "EDIT" of my previous post, but those yield the same, contextually correct, but graphically and stylistically broken, pages.

dari

4:26 pm on Dec 19, 2004 (gmt 0)

10+ Year Member



YAAAAAAAAAY...got it fixed.

Above all the rewrites for the .php files in the .htaccess file, i added:

RewriteRule ^templates/(.*)$ phpBB2/templates/$1

without the [L]

that fixed it :)