homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Redirecting a dynamic url containing Question Mark
Dynamic URL contains a "?" but having problems redirecting

 8:50 pm on Apr 18, 2009 (gmt 0)

I am trying to redirect just this particular dynamic URL from the www version, to the subdomain version.

An example of the page I want to redirect looks like this:




I tried this code, but it doesn't seem to be working:

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^wiki/index.php?title=Page_Name&action=edit$ "http\:\/\/wiki\.example\.com\/wiki\/index\.php\?title\=Page_Name\&action\=edit" [R=301,L]

Can anyone help me?



 9:23 pm on Apr 18, 2009 (gmt 0)

The \: and \/ and \. and \= syntax is wrong. You only need : or / or . or = in the target URL.

The URL, as visible to the rule (for matching in the pattern), does not contain the query string. You need another RewriteCond to look at that.

Your existing RewriteCond is redundant, or might need modifying to prevent a loop.


 10:19 pm on Apr 18, 2009 (gmt 0)

How about something like this:

RewriteCond %{HTTP_HOST} ^(www.example.com/wiki/index.php?title=Page_Name&action=edit$) [NC]
RewriteRule ^(.*)$ http:\\wiki.example.com/wiki/index.php?title=Page_Name&action=edit [R=301,L]

Still not working, but is this a better direction?


 10:28 pm on Apr 18, 2009 (gmt 0)

This is closer, but HTTP_HOST only sees the domain name.

You'll also need to look at QUERY_STRING to evaluate that.

Does the Rule need to look at every (.*) URL on your site?


 11:54 pm on Apr 18, 2009 (gmt 0)

The rule just needs to do a simple redirect of one page. I am having a hard time redirecting the page with the question mark in it :/ This simple rule is all I need:

RewriteRule ^wiki/index.php?title=Page_Name&action=edit $ [wiki.example.com...] [L,R=301]

The question mark throws this off though, even if I try to escape it.


 12:31 am on Apr 19, 2009 (gmt 0)

RewriteRule cannot directly see the query string parameters.

You need a RewriteCond that examines QUERY_STRING for this.


 1:15 am on Apr 19, 2009 (gmt 0)

All the information needed to make a working rule has been posted above, assuming that you refer to the Apache mod_rewrite documentation [httpd.apache.org]:

RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteCond %{QUERY_STRING} &?title=Page_Name&action=edit&?
RewriteRule ^wiki/index\.php$ http://wiki.example.com/wiki/index.php?title=Page_Name&action=edit [R=301,L]

Once that works, you may want to reconsider whether you really want to include the /wiki/index.php part in the URL: It would likely be easy (and better for users and search engines) to use a URL of wiki.example.com/?title=Page_Name&action=edit to refer to the file at /wiki/index.php.

There is simply no reason to "expose" the "file storage area" for your "wiki" subdomain in the URL. It makes correctly hearing, writing, remembering, and saying the URL that much more difficult, and is usually not necessary.

Note that the two "&?" subpatterns are "soft anchors, intended to prevent a match on a possible query of "subtitle=Page_Name&action=edit" or "title=Page_Name&action=edit-undo". Might save quite a bit of trouble in the future.



 3:22 am on Apr 19, 2009 (gmt 0)

Thank you all for your help. I wasn't aware that rewrite rules cannot see query string parameters, whoops. This problem has been solved :)

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