mod rewrite help!
am 03.01.2008 00:37:40 von rallykarro
Hi,
I have changed some url:s on my website.
Before a specific url was www.website.com/object.php?id=333 and now I
want to 301 redirect that with mod rewrite to www.website.com/object/333/
my rule looks like this:
RewriteCond %{REQUEST_URI} ^/object\.php
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^object\.php$ /object/%1/ [R=301,L]
When I hit www.website.com/object.php?id=333 I get redirected to
www.website.com/object/333/?id=333
Why does the ?id=333 get appended on my redirected url since I expect
the result to be www.website.com/object/333/
Is there any other way to specify a rule than does what I want?
Hope you can help me out!
thanks,
//Karolina
Re: mod rewrite help!
am 03.01.2008 02:12:56 von Jason Carlton
On Jan 2, 6:37=A0pm, rallyka...@hotmail.com wrote:
> Hi,
>
> I have changed some url:s on my website.
> Before a specific url waswww.website.com/object.php?id=3D333and now I
> want to 301 redirect that with mod rewrite towww.website.com/object/333/
>
> my rule looks like this:
> RewriteCond %{REQUEST_URI} ^/object\.php
> RewriteCond %{QUERY_STRING} ^id=3D(.*)$
> RewriteRule ^object\.php$ /object/%1/ [R=3D301,L]
>
> When I hitwww.website.com/object.php?id=3D333I get redirected towww.websit=
e.com/object/333/?id=3D333
> Why does the ?id=3D333 get appended on my redirected url since I expect
> the result to bewww.website.com/object/333/
>
> Is there any other way to specify a rule than does what I want?
>
> Hope you can help me out!
>
> thanks,
> //Karolina
I think that's supposed to be $1 instead of %1. I'm working on a
similar recipe right now, as a matter of fact.
- Jason
Re: mod rewrite help!
am 03.01.2008 02:25:00 von rallykarro
Hi,
Well, I guess when fetching parameters from the RewriteCond it is
supposed to be %, but when fetching parameters from the RewriteRule it
sholud be $.
In the example above I want the "id" fetched in the RewriteCond and
that's why I am using the %.
Let me know if you come up with something.
thanks,
//Karolina
Re: mod rewrite help!
am 03.01.2008 04:00:50 von Jason Carlton
On Jan 2, 8:25=A0pm, rallyka...@hotmail.com wrote:
> Hi,
>
> Well, I guess when fetching parameters from the RewriteCond it is
> supposed to be %, but when fetching parameters from the RewriteRule it
> sholud be $.
> In the example above I want the "id" fetched in the RewriteCond and
> that's why I am using the %.
>
> Let me know if you come up with something.
>
> thanks,
>
> //Karolina
Here's my second thought, but keep in mind that I'm pretty new at
working with Apache recipes.
You're rewriting object\.php with /object/%1/, and I'm not sure if the
$ at the end of \.php is supposed to mean "and everything after." I'm
pretty sure that this is where the problem is, but I'm not sure what
would fix it. Maybe:
RewriteRule ^object\.php* /object/%1/
Either way, it doesn't appear that the $ is reading everything to the
end, it's just overwriting the literal object\.php and leaving
everything that.
Re: mod rewrite help!
am 03.01.2008 05:39:24 von Jason Carlton
On Jan 2, 10:00=A0pm, Jason Carlton wrote:
> On Jan 2, 8:25=A0pm, rallyka...@hotmail.com wrote:
>
> > Hi,
>
> > Well, I guess when fetching parameters from the RewriteCond it is
> > supposed to be %, but when fetching parameters from the RewriteRule it
> > sholud be $.
> > In the example above I want the "id" fetched in the RewriteCond and
> > that's why I am using the %.
>
> > Let me know if you come up with something.
>
> > thanks,
>
> > //Karolina
>
> Here's my second thought, but keep in mind that I'm pretty new at
> working with Apache recipes.
>
> You're rewriting object\.php with /object/%1/, and I'm not sure if the
> $ at the end of \.php is supposed to mean "and everything after." I'm
> pretty sure that this is where the problem is, but I'm not sure what
> would fix it. Maybe:
>
> RewriteRule ^object\.php* /object/%1/
>
> Either way, it doesn't appear that the $ is reading everything to the
> end, it's just overwriting the literal object\.php and leaving
> everything that.
Since no one else has replied, here are just a few suggestions to try:
RewriteRule ^object\.php.*? /object/%1/
RewriteRule ^object\.php.*$ /object/%1/
RewriteRule ^object\.php.$ /object/%1/
I haven't tested any of these, but these variations seem to be the
most logical to me. I'm sure someone with more experience could tell
you right off, but I know it blows to be waiting around for help so I
hope these guesses help :-)
Re: mod rewrite help!
am 03.01.2008 06:52:18 von Jim Hayter
rallykarro@hotmail.com wrote:
> Hi,
>
> I have changed some url:s on my website.
> Before a specific url was www.website.com/object.php?id=333 and now I
> want to 301 redirect that with mod rewrite to www.website.com/object/333/
>
> my rule looks like this:
> RewriteCond %{REQUEST_URI} ^/object\.php
> RewriteCond %{QUERY_STRING} ^id=(.*)$
> RewriteRule ^object\.php$ /object/%1/ [R=301,L]
>
> When I hit www.website.com/object.php?id=333 I get redirected to
> www.website.com/object/333/?id=333
> Why does the ?id=333 get appended on my redirected url since I expect
> the result to be www.website.com/object/333/
>
> Is there any other way to specify a rule than does what I want?
>
> Hope you can help me out!
>
> thanks,
> //Karolina
>
Since you are sending this back to the browser with the 301, it is using
the new location with the original query string. To get the browser to
drop the query string, try this:
RewriteRule ^object\.php$ /object/%1/? [R=301,L]
I had a similar issue and if I remember correctly, putting that final ?
on the rewritten URL will cause the browser to use the new (empty) query
string.
HTH,
Jim
Re: mod rewrite help!
am 03.01.2008 07:08:22 von sean dreilinger
hello Karolina:
rallykarro@hotmail.com wrote:
> I have changed some url:s on my website.
> Before a specific url was www.website.com/object.php?id=333 and now I
> want to 301 redirect that with mod rewrite to www.website.com/object/333/
>
> my rule looks like this:
> RewriteCond %{REQUEST_URI} ^/object\.php
> RewriteCond %{QUERY_STRING} ^id=(.*)$
> RewriteRule ^object\.php$ /object/%1/ [R=301,L]
....
> Is there any other way to specify a rule than does what I want?
your idea should start working as-is if you change this:
RewriteRule ^object\.php$ /object/%1/ [R=301,L]
to this:
RewriteRule ^.*$ /object/%1/ [R=301,L]
but if you test a few URL variants on a live web server, you'll observe two gotchas:
1. the original query string will be appended to the final URL each time (which
may not be what you want if you're trying to clean up your URL architecture for
search engines or usability reasons).
2. as-is, this can't handle id=333 occuring anywhere in the query string except
at the very start. you want to correctly identify and capture id=333 even if it
occurs in the middle of a long string of cgi parameters like this:
/object.php?aid=AAA&id=333&cid=CCC
it may seem unlikely that your website or web server would inject any additional
cgi parameters to the query string, but you never know when you're going to
receive a link from a popular site with some embedded tracking info or other
information passed via cgi parameters that can interfere with your desire to see
id=333 at the very start of your query string.
to address these two gotchas, consider using this variation:
RewriteCond %{REQUEST_URI} ^/object\.php
RewriteCond %{QUERY_STRING} (?:^id|&id)=([^&]+)
RewriteRule ^.*$ /object/%1/? [redirect=permanent,last]
i tested those in an .htaccess file running under apache 2.2.6 on linux and it
works nicely. with your sample URL and with the proposed test query string.
hth
--sean :-)
--
sean dreilinger - http://durak.org/sean/
Re: mod rewrite help!
am 04.01.2008 01:21:57 von Rik Wasmus
On Thu, 03 Jan 2008 00:37:40 +0100, wrote:
> Hi,
>
> I have changed some url:s on my website.
> Before a specific url was www.website.com/object.php?id=3D333 and now =
I
> want to 301 redirect that with mod rewrite to www.website.com/object/3=
33/
>
> my rule looks like this:
> RewriteCond %{REQUEST_URI} ^/object\.php
> RewriteCond %{QUERY_STRING} ^id=3D(.*)$
> RewriteRule ^object\.php$ /object/%1/ [R=3D301,L]
>
> When I hit www.website.com/object.php?id=3D333 I get redirected to
> www.website.com/object/333/?id=3D333
> Why does the ?id=3D333 get appended on my redirected url since I expec=
t
> the result to be www.website.com/object/333/
>
> Is there any other way to specify a rule than does what I want?
RewriteCond %{REQUEST_URI} ^/object\.php
RewriteCond %{QUERY_STRING} ^id=3D(.*)$
RewriteRule ^object\.php$ /object/%1/? [R=3D301,L]
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
"Note: Query String
.....When you want to erase an existing query string, end the substitutio=
n =
string with just a question mark...."
-- =
Rik
Re: mod rewrite help!
am 04.01.2008 01:22:37 von Rik Wasmus
On Fri, 04 Jan 2008 01:21:57 +0100, Rik Wasmus =
wrote:
> On Thu, 03 Jan 2008 00:37:40 +0100, wrote:
>
>> Hi,
>>
>> I have changed some url:s on my website.
>> Before a specific url was www.website.com/object.php?id=3D333 and now=
I
>> want to 301 redirect that with mod rewrite to =
>> www.website.com/object/333/
>>
>> my rule looks like this:
>> RewriteCond %{REQUEST_URI} ^/object\.php
>> RewriteCond %{QUERY_STRING} ^id=3D(.*)$
>> RewriteRule ^object\.php$ /object/%1/ [R=3D301,L]
>>
>> When I hit www.website.com/object.php?id=3D333 I get redirected to
>> www.website.com/object/333/?id=3D333
>> Why does the ?id=3D333 get appended on my redirected url since I expe=
ct
>> the result to be www.website.com/object/333/
>>
>> Is there any other way to specify a rule than does what I want?
>
> RewriteCond %{REQUEST_URI} ^/object\.php
> RewriteCond %{QUERY_STRING} ^id=3D(.*)$
> RewriteRule ^object\.php$ /object/%1/? [R=3D301,L]
>
> http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
> "Note: Query String
> ....When you want to erase an existing query string, end the =
> substitution string with just a question mark...."
DOH! Newsserver lagged :P answer was allready given, je m'excuse.
-- =
Rik
Re: mod rewrite help!
am 04.01.2008 01:59:15 von rallykarro
On 4 Jan, 01:22, "Rik Wasmus" wrote:
> On Fri, 04 Jan 2008 01:21:57 +0100, Rik Wasmus
>
>
>
> wrote:
> > On Thu, 03 Jan 2008 00:37:40 +0100, wrote:
>
> >> Hi,
>
> >> I have changed some url:s on my website.
> >> Before a specific url waswww.website.com/object.php?id=333and now I
> >> want to 301 redirect that with mod rewrite to
> >>www.website.com/object/333/
>
> >> my rule looks like this:
> >> RewriteCond %{REQUEST_URI} ^/object\.php
> >> RewriteCond %{QUERY_STRING} ^id=(.*)$
> >> RewriteRule ^object\.php$ /object/%1/ [R=301,L]
>
> >> When I hitwww.website.com/object.php?id=333I get redirected to
> >>www.website.com/object/333/?id=333
> >> Why does the ?id=333 get appended on my redirected url since I expect
> >> the result to bewww.website.com/object/333/
>
> >> Is there any other way to specify a rule than does what I want?
>
> > RewriteCond %{REQUEST_URI} ^/object\.php
> > RewriteCond %{QUERY_STRING} ^id=(.*)$
> > RewriteRule ^object\.php$ /object/%1/? [R=301,L]
>
> >http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
> > "Note: Query String
> > ....When you want to erase an existing query string, end the
> > substitution string with just a question mark...."
>
> DOH! Newsserver lagged :P answer was allready given, je m'excuse.
> --
> Rik
Thank you very much. The rule works fine now.
//Karolina