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

301 redirect messed with rewrite rule

 12:21 pm on Feb 6, 2013 (gmt 0)

I know this must have an answer somewhere around here, but I am unable to find/locate it.

There is a typical 301 redirection rule in the same domain:
Redirect 301 /path/to/strange-url.htm /path/to/new/canonical-url.htm

Later on htaccess file, there is a rewrite rule for the canonical-url
RewriteRule ^([^/]+)/([^/]+)([^/]+)/([^-]+)-([^.]+)\.htm$ php/page.php?var1=$1&var2=$2&var3=$3&var4=$4&var5=$5 [NC,L]

Problem is that as soon as the 301 redirect activates, my new url becomes:

I tried the following, to prevent unnecessary loops, but it didn't worked:
RewriteCond %{REQUEST_URI} !^php/page.php
RewriteRule ^([^/]+)/([^/]+)([^/]+)/([^-]+)-([^.]+)\.htm?$ php/page.php?var1=$1&var2=$2&var3=$3&var4=$4&var5=$5 [NC,L]

How can I avoid this and maintain only the clean url?



 12:48 pm on Feb 6, 2013 (gmt 0)

If the redirect runs after the internal rewrite, then the previously rewritten internal filepath is exposed back out on the web as a new URL.

NEVER mix Redirect or RedirectMatch and RewriteRule in the same htaccess file.

Change all of the redirects to use RewriteRule syntax. The target URL of the redirect should include both the protocol and hostname too.

Make sure the redirects are listed before the rewrites.

Within each group, list from most specific to most general. The non-www/www canonicalisation redirect rule will be the last redirecting rule, and listed just before the first of the rewrites.

This will most likely fix your issues.

To suppress parameters on a redirect (when using a RewriteRule for the redirect) add a question mark to the end of the target URL.

Begin the target of each internal rewrite with a leading slash to prevent path injection especially when that target begins $1 or %1.


 3:03 pm on Feb 6, 2013 (gmt 0)

well that worked
- changed all Redirect 301 to RewriteRule [R=301,L]
- moved all former redirects before the actual rewrites

thank you for your assistance!

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