Forum Moderators: phranque

Message Too Old, No Replies

301 non-www to www redirect not working with dynamic URLs

         

javahava

10:06 am on Sep 21, 2005 (gmt 0)

10+ Year Member



I'm using the following code in .htaccess:

RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.example\.org [NC]
RewriteRule (.*) http://www.example.org/$1 [R=301,L]

It works fine for many links, which don't use dynamics variables, but for the ones that do have dynamic variables, things get weird. For example, I'll have a URL like this:

http://www.example.org?folder=5

My other code in my .htaccess would nicely translate the URL to something like:

http://www.example.org/f_5

Now, with the 301 non-www redirect, the URL again inserts back the dynamic character like? and = at the end. Any idea how to avoid this?

[edited by: jdMorgan at 7:07 pm (utc) on Sep. 22, 2005]
[edit reason] Example.com [/edit]

jdMorgan

2:47 pm on Sep 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



javahava,

There's nothing in the code you posted that would do this, but it's possible that an interaction with your 'other code' or with the client is doing it. If you can identify the portions of you code involved in rewriting a specific URL-path, then please post that URL-path, along with all rules related to rewriting it (after removing any specifically-identifiable information to comply with our TOS, of course).

Jim

javahava

10:01 am on Sep 22, 2005 (gmt 0)

10+ Year Member



hi, thanks for the help. i am currently using the following rewrite rule:

RewriteRule samplepage\.php\/s_([0-9]{1,})\/?$ /samplepage.php?imgcat=$1

This outputs a nice URL like this:

http://www.example.com/samplepage.php/s_1

However, when I add this code to the htaccess:

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]

The above URL turns to:

http://www.example.com//samplepage.php/s_1?imgcat=1

when i try the non www version. Any suggestions for getting it to look static again?

[edited by: jdMorgan at 7:08 pm (utc) on Sep. 22, 2005]
[edit reason] Example.com [/edit]

jdMorgan

7:05 pm on Sep 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You did not use an [L] flag on your internal script rewrite. So if the directives are in the order you posted them above, then the script rewrite will invoked first, changing the URL to the script call form and then the domain redirection rule will be applied to that URL. So, this is the expected behaviour.

To prevent that, use the directives in this order:


RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^/(.*) http://www.example.com/$1 [R=301,L]
#
RewriteRule samplepage\.php\/s_([0-9]+)\/?$ /samplepage.php?imgcat=$1 [L]

I assume you are running this code in httpd.conf, because of the double-slash in the redirected URL you posted above. I have adjusted the pattern in your redirect to correct for that.

Also, always use the [L] flag unless you *know* you don't want it.

Jim

javahava

10:00 am on Sep 23, 2005 (gmt 0)

10+ Year Member



ah - thank you! problem is fixed. i added this to the top of my .htaccess file (in root folder):

RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]

You were right - I had added the above code underneath some other rewrites, and that was what was causing the problem.