Forum Moderators: phranque

Message Too Old, No Replies

Can't get this to work!

         

ichthyous

4:17 am on Dec 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am trying to redirect all pages from an old directory: /Qstore/Qstore.cgi to http://example.com/store/index.php.

I tried the following: Redirect 301 /Qstore/Qstore.cgi http://example.com/store/index.php

but it simply adds the part of the url after? to the end of the new url, i.e. http://example.com/store/index.php?CMD=011&PROD=1143909939

So then I tried:

RewriteRule ^Qstore/Qstore\.cgi\?[.]$ http://example.com/store/index.php [R=301,L]

but it doesn't work. I need to redirect any page in the old Qstore/Qstore.cgi directory to store/index.php...how would I do this? Thanks!

phranque

7:47 am on Dec 3, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i would try changing this line:

RewriteRule ^Qstore/Qstore\.cgi\?[.]$ http://example.com/store/index.php [R=301,L]

to this:
RewriteRule ^Qstore/Qstore\.cgi\?.?$ http://example.com/store/index.php [R=301,L]

if i got this right, it should look for ...Qstore.cgi followed by a possible "?" and some possible characters following that to end the string.

jdMorgan

2:43 pm on Dec 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Query strings are not "seen" by RewriteRule. In order to test them, you must use a RewriteCond.

However, in this case, since all you want to do is to remove the query, just add a "?" to the end of your substitution URL:


RewriteRule ^Qstore/Qstore\.cgi$ http://example.com/store/index.p[b]hp?[/b] [R=301,L]

Jim

ichthyous

8:13 pm on Dec 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes that worked Jim, but I don't understand why. How does switching the? to the second url remove the query string? Thanks

jdMorgan

8:28 pm on Dec 3, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's by design. From the mod_rewrite RewriteRule documentation [httpd.apache.org]:

One more note: You can even create URLs in the substitution string containing a query string part. Just use a question mark inside the substitution string to indicate that the following stuff should be re-injected into the QUERY_STRING. When you want to erase an existing query string, end the substitution string with just the question mark.
(emphasis added)

Jim

ichthyous

2:33 pm on Dec 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's great to know...earlier this year I wrangled with query strings and never resolved it...now I know. Thanks again

ichthyous

9:05 pm on Dec 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Jim,

I tried to use the rule you provided earlier to get of stubborn? marks in some remaining urls, but it doesn't seem to work if there's no query string after it:

RewriteRule ^photos/washington-dc/$ http://example.com/photos/washington-dc/? [R=301,L]
RewriteRule ^photos/washington-dc/\?$ http://example.com/photos/washington-dc/ [R=301,L]
RewriteRule ^photos/washington-dc/?$ http://example.com/photos/washington-dc/ [R=301,L]

none of these stripped the? mark off the end of the url. Can urls with one? at the end and no quesry string be constued as a differnt URL tby the SEs? I.e.:

example.com/photos/washington-dc/? and
example.com/photos/washington-dc/

Thanks

jd01

10:56 pm on Dec 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, but query_strings are not 'seen' by RewriteRules.

See jdMorgan's post above. You will need to use a query_string condition to see if there is a query_string. IWO \? will not match the URL ending in ?.

To remove the query_string from all pages only when there IS A QUERY_STRING:

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

Hope this Helps.

Justin

jdMorgan

1:36 am on Dec 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem here is that there *is* a "?" on the requested URL, but no query parameters after it. Since the "?" is a boundary-marker and not part of either %{REQUEST_URI} or %{QUERY_STRING} as used in mod_rewrite, we get a rather bothersome condition which can't be solved with just those variables.

I'd suggest:


RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /photos/washington-dc/\?\ HTTP/
RewriteRule ^photos/washington-dc/$ http://www.example.com/photos/washington-dc/? [R=301,L]

and see if that works (even though it admittedly looks like it shouldn't). Once we can recognize this weird condition, redirecting with the trailing "?" on the substitution URL, the "?" should remove itself, basically.

Please post the results, as this is an odd boundary condition, and needs to go into my "remember this" list...

Jim

jd01

1:50 am on Dec 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is what I use in 'real life' to remove the query_string, from ? on.

RewriteCond %{THE_REQUEST} \?
RewriteRule .? http://www.example.com%{REQUEST_URI}? [R=301,L]

Just an idea.

Justin

(Removed NC from the cond. I let a couple strings through. =)

ichthyous

4:41 pm on Dec 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks for the response guys...Jim's first example did not seem to have any effect, Justin yours caused all sorts of problems, including images not loading and pages redirecting to the wrong place. Perhaps the problem is that this really is an issue with the php code that generates the dynamic urls. Rather than trying to patch it with htaccess perhaps it's better to solve the code problem if possible...thanks for your help though

jdMorgan

5:08 pm on Dec 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I tested that code myself, and now have it running on several sites. Did you flush your browser cache before testing?

I would suggest both using the code --if you can get it working-- and fixing the origin of the problem. Using the code will speed corrections in the search results (if those bad URLs are indexed).

Jim

ichthyous

9:22 pm on Dec 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes I tried it again and flushed the cache...it still has no effect. Perhaps the site's rewrite module's rewrite rules are preventing the change somehow? Here's the current state:

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} gallery\_remote2\.php
RewriteCond %{REQUEST_URI}!/index\.php$
RewriteRule . - [L]

RewriteCond %{QUERY_STRING} view=core.DownloadItem
RewriteCond %{QUERY_STRING} itemId=([0-9]+)
RewriteCond %{HTTP_REFERER}!://example.com/ [NC]
RewriteCond %{HTTP_REFERER}!^$
RewriteRule . /index.php?g2_view=rewrite.DownloadItem&g2_itemId=%1 [L]

RewriteCond %{THE_REQUEST} \ /sitemap(\?.¦\ .)
RewriteCond %{REQUEST_URI}!/index\.php$
RewriteRule . /index.php?g2_view=sitemap.Sitemap [QSA,L]

RewriteCond %{THE_REQUEST} \ /photos/([^?]+)/([0-9]*)(\?.¦\ .)
RewriteCond %{REQUEST_URI}!/index\.php$
RewriteRule . /index.php?g2_view=core.ShowItem&g2_path=%1&g2_page=%2 [QSA,L]

RewriteCond %{THE_REQUEST} \ /d/([^\/\?]+)(\?.¦\ .)
RewriteCond %{REQUEST_URI}!/index\.php$
RewriteRule . /index.php?g2_view=core.DownloadItem&g2_fileName=%1 [QSA,L]

</IfModule>

# END Url Rewrite section

jd01

9:30 pm on Dec 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would suggest trying to 'stip' the ? from original requests before any other rewrites/redirects take place. Order does matter.

If you do, make sure you redirect to any 'standardized' (www / non-www) domain at the same time to keep from multiple, unnecessary redirects.

Justin