Help with you know what

Using mod_rewrite to handle various content issues

Help with you know what

Postby austincb » Thu Jun 12, 2008 4:22 pm

Redirects!

I looked through about 50 posts in here to see if I could find anything but I'm still stuck. richardk, you're a rewrite machine!

I have a directory structure like this:

/httpdocs/.htaccess
/httpdocs/index.php
/httpdocs/mediawiki/

mediawiki not only accepts but USES two different URL formats depending on the link you click (stupid, right?)

Scenario 1:
Browser wants to see: subdomain.domain.com/mediawiki/index.php/Desired_Page
Would gets translated to:
subdomain.domain.com/index.php?wiki=Desired_Page

I have already gotten this to work with these rules:
Code: Select all
RewriteRule ^mediawiki/index.php/(.+)$ index.php?wiki=$1  [R=301]
RewriteRule ^mediawiki/index.php/(.+)/$ index.php?wiki=$1  [R=301]


Scenario 2:
In mediawiki, if you click the edit button on a page, it uses a different URL format:
subdomain.domain.com/mediawiki/index.php?title=Desired_Page&action=edit
Which I need to translate to:
subdomain.domain.com/index.php?wiki=Desired_Page&action=edit

Scenario 2 is where I am having my troubles!
I must be able to handle /mediawiki/index.php?* formats.

I have tried these with no success:

Code: Select all
RewriteRule ^mediawiki/index\.php\?title=(.+)$ index.php?wiki=$1  [R=301]
RewriteRule ^mediawiki/index.php?title=(.+)$ index.php?wiki=$1  [R=301]


The point of all of this is so I can include / render a mediawiki page within a div of another website.

Any assistance is greatly appreciated!
-Austin
austincb
 
Posts: 7
Joined: Thu Jun 12, 2008 4:21 pm

Postby richardk » Fri Jun 13, 2008 2:03 pm

Query strings are matched with RewriteConds and %{QUERY_STRING}. When using R=301, you should have the L flag too (unless there's a later rule that needs applying before the redirect happens, which is unlikely).

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Match the title value (in %2) and everything before (%1) and after it (also in %2).
RewriteCond %{QUERY_STRING} ^(.*&)?title(=.+)$ [NC]
# Match the filename and redirect to the new URL.
RewriteRule ^mediawiki/index\.php$ /index.php?%1wiki%2 [R=301,L]

RewriteRule ^mediawiki/index.php/(.*[^/])/?$ /index.php?wiki=$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby austincb » Fri Jun 13, 2008 6:44 pm

Thanks for the reply, Richard.
Unfortunately the code you provided seems to bork the site.
Doesn't give a 500 error but refreshing the page of a site takes a very long time and doesn't actually perform the redirect.

I've been using
Code: Select all
RewriteBase /
but commented it out and it didn't fix the problem.

I didn't know if the 2 rules you provided would cover for 'scenario 1' from the initial post so i got rid of them.

The .htaccess file I currently have is:

Code: Select all
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# Match the title value (in %2) and everything before (%1) and after it (also in %2).
RewriteCond %{QUERY_STRING} ^(.*&)?title(=.+)$ [NC]
# Match the filename and redirect to the new URL.
RewriteRule ^mediawiki/index\.php$ /index.php?%1wiki%2 [R=301,L]
RewriteRule ^mediawiki/index.php/(.*[^/])/?$ /index.php?wiki=$1 [R=301,L]


Any ideas?
austincb
 
Posts: 7
Joined: Thu Jun 12, 2008 4:21 pm

Postby austincb » Fri Jun 13, 2008 9:22 pm

Okay maybe I've narrowed it down to the first rule you gave (if I comment it out, the page at least loads):

Code: Select all
RewriteRule ^mediawiki/index\.php$ /index.php?%1wiki%2 [R=301,L]


The right-hand side of it seems weird to me..
/index.php?%1wiki%2

The rewritten URL should always be index.php?wiki=
I guess I don't get why the %1 is there

hmm...
austincb
 
Posts: 7
Joined: Thu Jun 12, 2008 4:21 pm

Postby austincb » Sat Jun 14, 2008 12:46 pm

If it is the case that the user tries to visit anything in the form of:

http://subdomain.domain.com/mediawiki/i ... hingelse=*

I just need to take the entire chunk of "Desired_Page&action=Desired_Action&&anythingelse=*" and place it right in front of: http://subdomain.domain.com/index.php?wiki=

I don't need to individually pick out the vars from the php argument... i'd be just fine with taking the entire string of chars after the ?title=

If I'm not being clear I apologize.
austincb
 
Posts: 7
Joined: Thu Jun 12, 2008 4:21 pm

Postby richardk » Sun Jun 15, 2008 11:05 am

The right-hand side of it seems weird to me..
/index.php?%1wiki%2

The rewritten URL should always be index.php?wiki=
I guess I don't get why the %1 is there

^(.*&)?title(=.+)$, it matches anything before "title" (into %1) and then anything after "title" (in %2), because you could get /mediawiki/index.php?action=edit&title=Desired_Page.

I didn't know if the 2 rules you provided would cover for 'scenario 1' from the initial post so i got rid of them.

Code: Select all
RewriteRule ^mediawiki/index\.php/(.*[^/])/?$ /index.php?wiki=$1 [R=301,L]

is for scenario 1.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?title(=.+)$ [NC]
RewriteRule ^mediawiki/index\.php$ /index.php?%1wiki%2 [R=301,L]

RewriteRule ^mediawiki/index\.php/(.*[^/])/?$ /index.php?wiki=$1 [R=301,L]


If you still get a 500 error, how does it get loaded into the <div>? If it's requested with the original URL, you might need to replace
Code: Select all
RewriteCond %{ENV:REDIRECT_STATUS} ^$

with
Code: Select all
RewriteCond %{QUERY_STRING} !^(.*&)?mod_rewrite=no(&.*)?$ [NC]

and request it with an extra parameter, mod_rewrite=no.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby austincb » Sun Jun 15, 2008 1:45 pm

^(.*&)?title(=.+)$, it matches anything before "title" (into %1) and then anything after "title" (in %2), because you could get /mediawiki/index.php?action=edit&title=Desired_Page.


Ah, that makes sense, but none of mediawiki's code would ever form a link like that, and I'm not especially worried about any hostile users.

As for the new code you gave, it's not a 500 error per say... It just 'hangs'... Those aren't the same thing though, right?

I've been posting for help in a few other places, and like you they give me some code to try and every single time the 'scenario 2' attempt causes some kind of hang. Maybe it's a server issue but that doesn't quite make sense to me, as some of the rewrite rules (like for scenario 1) work fine.

I'm beginning to see why mod_rewrite has become notorious.
austincb
 
Posts: 7
Joined: Thu Jun 12, 2008 4:21 pm

Postby richardk » Sun Jun 15, 2008 2:57 pm

The problem is almost certainly that it's causing a loop. MediaWiki outputs /mediawiki/index.php?action=edit&title=Desired_Page, mod_rewrite redirects it to /index.php?action=edit&wiki=Desired_Page, your index.php requests /mediawiki/index.php?action=edit&title=Desired_Page, mod_rewrite redirects it to /index.php?action=edit&wiki=Desired_Page, your index.php requests...

What are you doing to get the content into the <div>? Are you requesting the MediaWiki URL? Using
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{QUERY_STRING} !^(.*&)?mod_rewrite=no(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?title(=.+)$ [NC]
RewriteRule ^mediawiki/index\.php$ /index.php?%1wiki%2 [R=301,L]

RewriteRule ^mediawiki/index\.php/(.*[^/])/?$ /index.php?wiki=$1 [R=301,L]

and requesting /mediawiki/index.php?action=edit&wiki=Desired_Page&mod_rewrite=no in index.php will stop any loops.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby austincb » Sun Jun 15, 2008 3:36 pm

index.php (the real one -- not the mediawiki one) includes content_loader.php

content_loader.php then has switch statements.
I have a switch block for non-wiki pages ($_GET['p']), and one after that for wiki pages ($_GET['wiki']).

Here is the wiki switch block:
Code: Select all
else if ($_GET['wiki'])
{
echo '<div id="pageContent">';
$include = 'http://subdomain.domain.com/mediawiki/index.php?title='.$_GET['wiki'];
if ($_GET['action'])
{
$include .= '&action='.$_GET['action'];
}
include $include;
echo '</div>';
}


Thus the above echoes and includes place the desired page into a container on the main site, and it works fine for 'scenario 1'.

This is why I believe an external redirect is needed. an entire new http request comes in with the newly formatted url, which allows my content_loader to do it's thing.

I don't know how to insert a &mod_rewrite=no ... would that go in the rewrite rule right after the 2 conditions?
austincb
 
Posts: 7
Joined: Thu Jun 12, 2008 4:21 pm

Postby richardk » Sun Jun 15, 2008 3:45 pm

Replace
Code: Select all
$include = 'http://subdomain.domain.com/mediawiki/index.php?title='.$_GET['wiki'];

with
Code: Select all
$include = 'http://subdomain.domain.com/mediawiki/index.php?mod_rewrite=no&title='.$_GET['wiki'];

You don't have to make any changes to the mod_rewrite (from my last post).

You can change "mod_rewrite=no" if you want, but make sure you change it in the mod_rewrite (the first RewriteCond) and in the PHP (the line above).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Content

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron