简体   繁体   中英

Having trouble with some redirect/rewrite codes .. 301 and 410s

Brand new here and totally NOT a coder, so be gentle. The level of understanding I have here is about that of a toddler, so pretend you're talking to a 5 year old and I should be able to keep up.

I'm switching to a new server, and no longer using coppermine gallery. I can't get the redirects from the old cpg galleries and images to work.

For albums and categories that I will not redirect

(The url I want to redirect here would be) http://www.example.com/stock/thumbnails\.php\?album=62

They're gone and no longer exist, I wrote the 410 rule as

RewriteRule ^stock/thumbnails\.php\?album=62$ - [R=410, L]

That breaks the new site and creates a 500 error

For old albums that I want to redirect to a new url, such as this url http://www.example.com/stock/thumbnails\.php\?album=3

I wrote

RewriteRule ^stock/thumbnails\.php\?album=36$ https://www.example.com/gallery/appalachian-trail-photos/ [R=301,L]

But it does nothing. The urls show as 404 pages. I also tried it as

Redirect 301 /stock/index.php?cat=2 https://www.example.com/gallery/outdoor-recreation-photos/

which also does nothing.

I also want to redirect any image display pages to the root gallery of their relative album. So an image from a particular cpg album would go to the gallery page for that on the new site. A url like this

http://www.example.com/stock/displayimage.php?album=1&pid=4563#top_display_media

I wrote the redirect as

RewriteRule ^stock/displayimage\.php\?album=1&.*$ https://www.example.com/gallery/canadian-wildlife-photos/ [R=301,L]

RewriteRule ^stock/displayimage\.php\?album=32&.*$ - [R=410,L]

to send any image from album 1 to the CA wildlife photos album and any image from album 32 is gone.

None of these last work but I can't see what's wrong with them?

Any help would be superduper appreciated, thanks. Apologies in advance for my ignorance.

 RewriteRule ^stock/thumbnails\.php\?album=62$ - [R=410, L] ^--ERROR HERE

The erroneous space in the flags argument is likely the cause of the 500 error. This is a syntax error, so just "breaks". It should be [R=410,L] (no space). The L flag is actually redundant here when you specify a non-3xx status code, so this is the same as simply [R=410] , which can be further simplified to just [G] . The G flag is shorthand for R=410 (ie. "410 Gone").

However, the RewriteRule pattern (first argument) matches against the URL-path only. This does not match against the query string (the part of the URL after the first ? ). To match the query string you need a separate condition ( RewriteCond directive) and match against the QUERY_STRING server variable (which does not include the ? prefix).

For example, the above should be written something like:

RewriteCond %{QUERY_STRING} ^album=62$
RewriteRule ^stock/thumbnails\.php$ - [G]

This matches the URL /stock/thumbnails.php?album=62 exactly . And serves a "410 Gone". If there are any other URL parameters (in the query string) then the match will fail. eg. /stock/thumbnails.php?foo=1&album=62 will not match.

To match the URL parameter album=62 anywhere in the query string (if there are other URL params) then tweak the CondPattern (2nd argument to the RewriteCond directive) like so:

RewriteCond %{QUERY_STRING} (^|&)album=62(&|$)
:

The additional alternation subgroups (^|&) and (&|$) before and after the URL parameter, ensure we only match that exact URL parameter and not fooalbum=62 or album=623 , etc.

 RewriteRule ^stock/displayimage.php?album=1&.*$ https://www.example.com/gallery/canadian-wildlife-photos/ [R=301,L]

The above points should resolve the main issue with this rule (matching the query string). However, you also need to add the QSD flag in order to remove the original query string from the redirect response. Otherwise, the original query string (ie. album=1&.... ) will be appended to the end of target URL (eg. /gallery/canadian-wildlife-photos/?album=1&... ).

For example:

RewriteCond %{QUERY_STRING} (^|&)album=1(&|$)
RewriteRule ^stock/displayimage\.php$ https://www.example.com/gallery/canadian-wildlife-photos/ [QSD,R=301,L]
 Redirect 301 /stock/index.php?cat=2 https://www.example.com/gallery/outdoor-recreation-photos/

Note that the (mod_alias) Redirect directive does not match the query string either (only the URL-path). To match the query string you need to use mod_rewrite ( RewriteRule and RewriteCond ) as mentioned above.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM