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

Apache Web Server Forum

Single file redirect

 3:37 pm on Jun 3, 2010 (gmt 0)

Well, I'm a bit lost here as this code is not working:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^main.php?section=oldpage$ /newpage/ [R=301,L]

Forgive my blindness but I stared at this piece of code for hours now without getting it...



 6:45 pm on Jun 3, 2010 (gmt 0)

What is it meant to to do?

RewriteRule can only see the path part of the URL.

You will therefore need a preceding RewriteCond looking at QUERY_STRING.

The redirect target should include the protocol and domain name.


 8:50 pm on Jun 3, 2010 (gmt 0)

Damn, I knew I was way off here. So thanks for your heads up.

I guess this will work then:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{QUERY_STRING} ^section=(oldpage*)$
RewriteRule main.php ht tp://www.domain.com/newpage/ [L,R=301]


 9:14 pm on Jun 3, 2010 (gmt 0)

The rule as written will 'carry forward' the originally-requested query string. Is that what you want to do?

Again, what exactly is this rule meant to do? What is the (full) requested URI-path to be redirected, and what is the full URI to be redirected to?

"It will work" is only about 10% of the way to "It will work properly with no unintended side effects," and that is not good enough; For optimal results, nail down your requirements firmly before we start coding.



 9:42 pm on Jun 3, 2010 (gmt 0)

Thanks for your response.

Basically I want to redirect the old php-file to a new file (which is now in a directory-based CMS):

ht tp://domain.com/main.php?section=oldpage


ht tp://domain.com/newpage/


 10:01 pm on Jun 3, 2010 (gmt 0)

In that case, you "bascially" need to clear the query string using the "?" operator on the substitution URL:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^section=oldpage-string$
RewriteRule ^main\.php$ http://www.example.com/newpage/? [L,R=301]

Note othe minor corrections to literal query string value, anchoring, and literal-character escaping in rule pattern.

You can use the mod_rewrite documentation at apache.org to determine if this is exactly what you want to do.



 10:07 pm on Jun 3, 2010 (gmt 0)

Thanks Jim, works like a charm.


 10:47 pm on Jun 3, 2010 (gmt 0)

What happens if you ask for the non-www version of the old URL, and/or if you append extra parameters?

Does it then still do exactly what you want?

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