Forum Moderators: phranque

Message Too Old, No Replies

redirects in .htaccess

redirect htaccess

         

cyberdyne

10:08 pm on Mar 6, 2008 (gmt 0)

10+ Year Member



Hi
I have had a heck of a lot of hits on a .php file located in my coppermine image gallery. However, the file is useless to anyone without a specific image name, which isn't being specified in the numerous direct hits.

So, I am trying to add a redirect to my .htaccess to redirect any hits on the file (imagedisplay.php) to the gallery root.

I tried the following as a simple redirect;

RedirectMatch permanent ^/coppermine/displayimage.php$ /coppermine/

However, this also redirects all requests for actual images, eg:

..../coppermine/displayimage.php?album=random&cat=17&pos=-455

Therefore, my question is how to add the redirect ot the .htaccess file so that it redirects ONLY those requests that ARE NOT genuine and DO NOT end in; ?album=random&cat=XX&pos=-#*$!

Thanks for any help.

jdMorgan

10:25 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use mod_rewrite, and use a RewriteCond to check the query string to be sure it's not blank or invalid.

Jim

cyberdyne

10:31 pm on Mar 6, 2008 (gmt 0)

10+ Year Member


Thank you Jim.

However, I'm afraid my knowledge of Apache isn't that good sorry.

Although, referring to current examples, I'm guessing it would be something like (?):

RewriteCond %{HTTP_HOST} ^mysite.blah/coppermine/displayimage.php$ [OR]
RewriteCond %{HTTP_HOST} ^www.mysite.blah/coppermine/displayimage.php$
RewriteRule ^(.*)$ http://www.mysite.blah/coppermine/ [R=301,L]

Thank you.

jdMorgan

10:47 pm on Mar 6, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, go to the mod_rewrite documentation [httpd.apache.org], look at the RewriteCond directive, and find the name of the server variable you need to use to check and validate the query string.

Specific questions are most welcome, but please read the docs for the basics.

Our forum charter (link at top left above) also contains links to several useful resources.

Thanks,
Jim

cyberdyne

11:06 pm on Mar 6, 2008 (gmt 0)

10+ Year Member



Understood.

Thanks

cyberdyne

12:13 am on Mar 7, 2008 (gmt 0)

10+ Year Member



Well, I'm trying !
:)

1.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^/coppermine/displayimage.php$. [or]
RewriteCond %{QUERY_STRING} ^/coppermine/thumbnails.php$.
RewriteRule ^$ /coppermine/? [R=301,L]

2.
RewriteEngine on
RewriteCond %{query_string} ^/coppermine/displayimage.php$
RewriteRule ^$ /coppermine/ [R=301,L]

To no avail.... yet....

jdMorgan

12:39 am on Mar 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should follow your own logic -- From your first post:

# If NOT valid query
RewriteCond %{QUERY_STRING} !^album=[^&]+&cat=[^&]+&pos=.+$
# redirect from /coppermine/displayimage.php to /coppermine/
RewriteRule ^coppermine/displayimage\.php$ http://www.example.com/coppermine/ [R=302,L]

This code is intended for use in www.example.com/.htaccess

Jim

cyberdyne

8:44 am on Mar 7, 2008 (gmt 0)

10+ Year Member



Thank you Jim.

I have tried the above but unfortunately, it did not work.
There was no error message or error page, the '/displayimage.php' page simply remained visible without being redirected.
I tried the code with 'Options +FollowSymlinks' too, but to no avail.

Thank you for your time.

jdMorgan

3:11 pm on Mar 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) Did you completely flush your browser cache before testing?
2) Where do you put the code -- See note in above post.

Jim

cyberdyne

3:20 pm on Mar 7, 2008 (gmt 0)

10+ Year Member



Hi
I did indeed flush my browser cache completely and have done so again since.

The file resides in my root .htaccess file and the code is at the top of the file, just below my error page settings.

The exact code reads:

RewriteEngine On
RewriteCond %{QUERY_STRING} !^album=[^&]+&cat=[^&]+&pos=.+$
RewriteRule ^http://www.mysite.blah/coppermine/displayimage\.php$ http://www.mysite.blah/coppermine/ [R=302,L]

(mysite.blah is of course replaced with my correct domain)

I've just tried the above code again and still no luck.
Thank you for your time and patience Jim and sorry to trouble you.

jdMorgan

3:52 pm on Mar 7, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, but you're making changes to the code which break it. In www.example.com/.htaccess, it must read:

RewriteEngine on
RewriteCond %{QUERY_STRING} !^album=[^&]+&cat=[^&]+&pos=.+$
RewriteRule ^coppermine/displayimage\.php$ http://www.example.com/coppermine/ [R=302,L]

If you have any problems with that exact code, then let us know -- There are many other side-issues which can cause problems, such as Alias directives which can interfere with the rewrites. But we cannot support you if you make changes and then report that "it doesn't work" without telling us up-front you made changes.

Be aware that one single character out of place in mod_rewrite can completely break your server; One little typo can cause a problem you may not find for months -- after your search rankings have been devastated. Make no changes without reading the documentation, reviewing the examples, and thoroughly understanding what those changes mean. There is no "freestyle" coding in .htaccess -- The code is lean and mean and totally unforgiving of errors.

Jim

cyberdyne

4:27 pm on Mar 7, 2008 (gmt 0)

10+ Year Member



My apologies.
I didn't even intend to write the changes in my above post, I simply copy and pasted the wrong section of script.

I'll try it again and try not to bother you again.
Thanks very much.

g1smd

8:11 pm on Mar 7, 2008 (gmt 0)

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



Once you're sure what HTTP_HOST, THE_REQUEST, REQUEST_URI, and QUERY_STRING are testing, things will begin to fall into place. Knowing which one to use, and when is all part of the learning process.

See also: [webmasterworld.com...]