Forum Moderators: phranque
First I'd like to say that I'm new here, and I've spent the last 5 days on numerous forums trying to get help with some rewrite rules... with no success. I'm at my wits end!
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: http://subdomain.example.com/mediawiki/index.php/Desired_Page
Would gets translated to:
http://subdomain.example.com/index.php?wiki=Desired_Page
I have already gotten this to work with these rules:
RewriteRule ^mediawiki/index.php/(.+)$ index.php?wiki=$1 [R=301,L]
RewriteRule ^mediawiki/index.php/(.+)/$ index.php?wiki=$1 [R=301,L]
Scenario 2:
In mediawiki, if you click the edit button on a page, it uses a different URL format:
http://subdomain.example.com/mediawiki/index.php?title=Desired_Page&action=edit
Which I need to translate to:
http://subdomain.example.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:
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.
Now like I've said, I tried getting help other places. One fellow supplied me with this chunk of code:
# 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]
But it ended up causing some kind of loop (the website hangs).
I see he used the rewrite condition and query_string... isn't it possible to just move the entire query string to a different URL?
So just to sum up: If it is the case that the user tries to visit anything in the form of:
http://subdomain.example.com/mediawiki/index.php?title=Desired_Page&action=Desired_Action&anythingelse=*
I just need to take the entire query string "Desired_Page&action=Desired_Action&&anythingelse=*" and place it right in front of: http://subdomain.example.com/index.php?wiki=
I don't need to individually pick out the vars from the query string.
Sorry for the long-winded post.
Any assistance is greatly appreciated!
You are my last hope, webmasterworld!
-Austin
[edited by: jdMorgan at 5:46 pm (utc) on June 15, 2008]
[edit reason] example.com, disabled smilies in code [/edit]
In both cases, I show internal rewrites, not external redirects. Redirects inform the client of a new URL, slow things down by requiring the client to make a second HTTP request to fetch the page, and should not be necessary for what you're trying to do.
If you have problems with images, css, or external JS files not loading, change the references to these objects to use server-relative or canonical URLs. That is, for an image, use <img src="/images/logo.gif"> or <img src="http://example.com/images/logo.gif">, and do not use <img src="images/logo.gif">
# Scenario 1:
# Replace first ruleset with a single rule with [i]optional[/i] trailing slash, change from redirect to internal rewrite:
RewriteRule ^mediawiki/index.php/([^/]+)/?$ /index.php?wiki=$1 [L]
#
# Scenario 2:
# query string must be tested using RewriteCond; It is not "seen" by RewriteRule
RewriteCond %{QUERY_STRING} ^title=([^&]+)&action=edit$
RewriteRule ^mediawiki/index\.php$ /index.php?wiki=%1&action=edit [L]
The first rule you gave works, however I do believe I need an external redirect, as I think my php code running in index.php needs a 'fresh start'. I have a main content <div> and a switch statement on the $_GET variable... depending on the type of variable it will include the appropriate address.
The 'content loader' looks like this:
else if ($_GET['wiki'])
{
echo '<div id="pageContent">';
$include = 'http://subdomain.example.com/mediawiki/index.php?title='.$_GET['wiki'];
if ($_GET['action'])
{
$include .= '&action='.$_GET['action'];
}
include $include;
echo '</div>';
}
For the scenario 2 code you gave me, it unfortunately causes a hang...
Here is my .htaccess as it stands now:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# Scenario 1:
# Replace first ruleset with a single rule with optional trailing slash, change from redirect to internal rewrite:
RewriteRule ^mediawiki/index.php/([^/]+)/?$ /index.php?wiki=$1 [L]
# Scenario 2:
# query string must be tested using RewriteCond; It is not "seen" by RewriteRule
RewriteCond %{QUERY_STRING} ^title=([^&]+)&action=edit$
RewriteRule ^mediawiki/index\.php$ /index.php?wiki=%1&action=edit [L]
A gentleman from somewhere else helped me resolve this with the use of an extra variable to prevent the looping.
As far as I can tell right now, everything works the way I want it now.
Here's the .htaccess code:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} !^(.*&)action=submit(&.*)?$ [NC]
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]
[edited by: encyclo at 2:07 am (utc) on June 16, 2008]
[edit reason] disabled smilies [/edit]