Forum Moderators: phranque
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]
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
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]
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]
Also, always use the [L] flag unless you *know* you don't want it.
Jim
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.