Redirect from image to script - .htaccess

please help me with redirect in .htaccess
Iam have simple link
http://domain.com/test123/image-123.gif
Iam need to redirect from this image to script.
http://domain.com/test.php?company=test123?image=image-123.gif
My example doesnt work =(
RewriteEngine On
RewriteRule ^(.+)/(.+).\gif$ tracking.php?company=$1&email=$2 [L,QSA]

Assuming this is literally what you have in a .htaccess file, the problem is that you have .\g rather than .g -- that is, you've escaped the 'g' character, rather than the . character.
That is, what you want is:
RewriteRule ^(.+)/(.+)\.gif$ tracking.php?company=$1&email=$2 [PT,L,QSA]
Also, note that this does not redirect, as you say in your question, but it does a transparent rewrite. If you want it to redirect, replace the PT with a R in the flag.

Related

htaccess to add ?query=string to specific url

I'm pulling my hair out trying to get a .htaccess rewrite rule to work. I'm sure this should be easy but I just can't get it right!
I need to add ?query=string to a specific URL pattern so:
www.example.com/downloads/file
Becomes:
www.example.com/downloads/file?query=string
The best I can come up with is:
RewriteRule ^downloads/(.*) downloads/$1?query=string
But it's not working. Where am I going wrong?
What you have looks OK, except you might need to include the L flag to prevent further rewritting:
RewriteRule ^downloads/(.*) downloads/$1?query=string [L]
And if this is intended to be an external redirect then you'll need to make the substitution absolute or root-relative and include the R flag:
RewriteRule ^downloads/(.*) /downloads/$1?query=string [R=302,L]

Ho do I write an .htaccess redirect for a directory containing an ellipsis

Some how I had an invalid directory indexed in Google, and because of some dynamic relative links I now have 2500 "missing" pages indexed. I'm trying to use an .htaccess 301 redirect to correct the problem but I can't seem to get it to work. I need to redirect www.domain.com/shop/pc/.../pc/filename.asp to www.domain.com/shop/pc/filename.asp.
The rule I have written that doesn't want to work is RewriteRule ^shop/pc/\.\.\./pc/(.*)$ /shop/pc/$1 [R=301,L]
Any thoughts?
mod_rewite uses PCRE, so for these Unicode characters (I included the two dot leader as well, since I imagine that is more likely to sneak into a URL than an ellipsis):
# U+2026 … \xe2\x80\xa6 HORIZONTAL ELLIPSIS
RewriteRule ^shop/pc/\xe2\x80\xa6/pc/(.*)$ /shop/pc/$1 [R=301,L]
# U+2025 ‥ \xe2\x80\xa5 TWO DOT LEADER
RewriteRule ^shop/pc/\xe2\x80\xa5/pc/(.*)$ /shop/pc/$1 [R=301,L]
Note you may need the [B] flag (see flags) if the browser is percent-escaping the ellipsis.

htaccess rewrite, url doesn't work when i add a / at the end

I have got a problem that seems to me something simple but i'm new to htaccess and can't find the solution.
i've got the following line in my htaccess (root map)
RewriteRule ^page1/([a-zA-Z0-9_-]+)/$ page1.php?name=$1
RewriteRule ^page1/([a-zA-Z0-9_-]+)$ page1.php?name=$1
When i enter the the following url it works without a problem
www.myexample.com/page1/variable
the strange thing happens when I add a / at the end. Then the page can't get the GET value out of the URL.
Thanks for your time and help!
Get rid of the ending /$ sign on the first rule
RewriteRule ^page1/([a-zA-Z0-9_-]+) page1.php?name=$1
Or you can continue to capture data
RewriteRule ^page1/([a-zA-Z0-9_-]+)/(.*)$ page1.php?name=$1
Ultimately if you want to keep capturing more data with "/" as a delimiter, I suggest doing
RewriteRule ^page1/(.*)$ page1.php?url=$1
Then use the server side script to determine what to do.

.htaccess Redirect sub-folder

Hi I'm not a programmer by any stretch of the imagination and am trying to do a multi 301 redirect in my htaccess file based on the following:
So I have a ton of urls all with similar naming conventions - here is a sample of 2.
http://www.hollandsbrook.com/garrett-at-gold/
http://www.hollandsbrook.com/garrett-ace-250/
These urls need to redirect to:
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-at-gold/
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-ace-250/
I could just redirect them 1 line at a time, but I'd like to use regex.
Here's what I was thinking so far but not working:
RewriteRule ^garrett-([a-z])/$ /garrett-metal-detectors/$1/ [R]
Basically i need to redirect any page right off the root that starts with "garrett-" to include the folder path of "garrett-metal-detectors".
Any thoughts would be MUCH appreciated. Many thanks in advance for your help.
if you want temprorary redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=302,L]
if you want permanent redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=301,L]
I'm am not an expert on Regular Expressions, but looks like your reg ex may be a bit off...
try:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^((garrett)(-[a-z0-9]).*)/$ /metal-detectors/$1/ [R]
This is looking fro anything starting with "garrett" followed by any letter/number/hyphen combo.
Note: having "garett" in the destination part give you a loop of redirects, so you may have to choose a different word, or remove it all together...

How to insert an "index.php" into every url using .htaccess?

Example: My Site gets called like that:
www.mysite.com/controller/method/parameter1/parameter2
Now, .htaccess needs to rewrite this URL into:
www.mysite.com/index.php/controller/method/parameter1/parameter2
But the problem is: In case of an img, css or js directory, no redirection should happen.
How can I achieve this? What must I put to .htaccess? I just added this line but nothing happens:
RewriteCond $1 !^(css|js|images)
I haven't tested it, but this should work:
RewriteRule !^((css|js|images)/.*)$ index.php%{REQUEST_URI} [L, NE]
%{REQUEST_URI} will be the original /controller/method... stuff, including the ?query part hopefully. NE prevents double escaping of stuff, and L means no further rules are applied.

Resources