Forum Moderators: phranque

Message Too Old, No Replies

htaccess redirect with 2 parameters

keywords from only one site

         

verbatim

2:46 am on Nov 12, 2011 (gmt 0)

10+ Year Member



i am able to redirect a url with the keyword in it to the desired url with the following code:


RewriteRule ^(.*)word(.*)$ http://mysite.com [R=302,L]


the problem is when a link on my page is clicked with the word in it it loops.


I want links from a 2 particular sites with certain words to be redirected to new urls. Example:

referrersite.com/wq3d-word1-asd redirects to mysite.com/word1
referrersite2.com/wq3d-word2-awe redirects to mysite.com/word2
referrersite2.com/wq3d-word2-awe redirects to mysite.com/word1

Even if I were able to do this with words referred from one site I would be happy!

At one point I had code that slowly went to a wordpress page with a 302 error that read" this page has been permanently moved. click here. In all of my alterations I do not even have that code any more. My current code is below but it is giving me an error.

RewriteCond %{HTTP_REFERER} ^http://(www\.)?referrersite.com/(.*)word(.*)$ [NC]
RewriteRule \.*$ http://www.mysite.com/2011/10/19/word/ [L]


Any help offered would be greatly appreciated.

Thanks in advance

wilderness

3:05 am on Nov 12, 2011 (gmt 0)

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



There's more than one was to skin a cat.
Many times folks are looking for wildcard syntax methods (because they are discussed here so frequently), when KISS methods will function as well.

RewriteCond %{HTTP_REFERER} referrersite
RewriteCond %{HTTP_REFERER} (word1|word2)
RewriteRule blah-blah-blah

IMO, these regex's to encompass multiple input or output simply confuses the newbies, whom don't even understand the basic principles of httaccess.
KISS.

lucy24

6:20 am on Nov 12, 2011 (gmt 0)

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



At one point I had code that slowly went to a wordpress page with a 302 error that read" this page has been permanently moved. click here.


A 302 is not an error. It means "temporarily moved". Unfortunately it is the default Redirect, both in mod_alias and mod_rewrite, even though it's almost never appropriate-- unless you really are sending people elsewhere for a week or two while you do some major housekeeping. Normally, you'll want a 301. In mod_rewrite that means an explicit flag [R=301]

What you're describing sounds like a page-level redirect. You don't need it unless you have lots of visitors who had the old address bookmarked and you want to be sure that they notice the change. Otherwise, a quick and invisible .htaccess redirect is cleaner.

My current code is below but it is giving me an error.

RewriteCond %{HTTP_REFERER} ^http://(www\.)?referrersite.com/(.*)word(.*)$ [NC]
RewriteRule \.*$ http://www.example.com/2011/10/19/word/ [L]


Error? Really? I'd expect it to redirect everything in sight, since the \.*$ means "anything that ends in zero or more literal periods" -- in other words, anything at all, just as if you'd said it without the \ escape. What type of error do you get?

As wilderness said, if you're looking for two separate elements in your referer, and it doesn't matter what comes between them, it's easier simply to search separately for each piece. It may even be faster for the computer ;) And note that in your Rule as originally given, you don't need parentheses around the (.*) since you are not capturing them.

(word1|word2)


Or

word[12]


depending on personal preference.

***

Cats need not enter into it, except that they should be kept far far from the keyboard when you are working on htaccess. Only yesterday, something with 4 legs introduced several thousand blank lines into a document. Fortunately not .htaccess, but the principle holds.

g1smd

7:35 am on Nov 12, 2011 (gmt 0)

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



Never use .* at the beginning of a RegEx pattern.

If you're not re-using the backreference data then the trailing .* is also redundant.

For the rule target, the URL for domain root should end with a trailing slash.

verbatim

3:23 am on Nov 13, 2011 (gmt 0)

10+ Year Member



wilderness:greatly appreciated! Looking back I was over complicating something very simple.
The code below would have worked, but i found that the referring site (google) uses javascript code that I believe takes their name out of the string.

RewriteCond %{HTTP_REFERER} referrersite
RewriteCond %{HTTP_REFERER} (word1|referrersite name)
RewriteRule blah-blah-blah


I then thought that if any site besides mine linked to the page i could initiate the rewrite rule with the following code:

RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://www\.mysite\.com [NC]
RewriteRule ^(word)$ http://mysite.com/2011/10/19/word/ [R=302,L]


I had the above code [or a version of it] working properly but in all my re-edits lost the working code. If ANYONE could help me get it to work properly i would greatly appreciate it!

lucy: Thanks for the reply, it was very informative. I tried to use the code I said was giving me an error but now when I use it it doesn't redirect to the word, it just goes dorectly to the page it links to. I have no idea why that is.

g1smd: thanks! after reading your reply I changed it to
 RewriteRule (word) http://mysite.com [R=302,L] 
or something similar, maybe
 RewriteRule ^(word)$ http://mysite.com [R=302,L] 
and it worked when I'm redirecting to any site besides my own. I once had it redirecting correctly to a page on my site but did some additional editing and lost that code. The lesson I learned was "if you get a working code, copy it in another file if you are going to continue editing."

I have tried NUMEROUS variations and can not get it to redirect to the correct page on my site again. Any help offered would be appreciated.

Thanks again very much everyone!

lucy24

7:17 am on Nov 13, 2011 (gmt 0)

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



RewriteCond %{HTTP_REFERER} .
RewriteCond %{HTTP_REFERER} !^http://www\.mysite\.com [NC]
RewriteRule ^(word)$ http://mysite.com/2011/10/19/word/ [R=302,L]


The first line may not do what you want it to do. You're looking for anything that has a referer, right? In general, a null referer comes through as - which counts as . in RegEx. You might actually be better off with \. meaning that the referer contains at least one literal period somewhere. Like .com or www. or .uk or... et cetera.

The pattern itself may be too tightly constrained.

^word$

(you don't need to capture, since you're not reusing it) will only work if the user requests exactly

www.example.com/word

without trailing slash, without "index.html" or equivalent-- and, of course, it can only be a top-level file. The exact anchoring will have to depend on the actual words: for example, if you wanted to capture /word/ but not /swordfighting/ :)

Are you positive you want a temporary redirect?

I don't know if this is at all a useful analogy, but I'm currently redirecting anyone from a specific region who searches for a specific term.

:: shuffling papers ::

It looks like this, minus the identifying details:

RewriteCond %{HTTP_REFERER} \.(aa|bb)/
RewriteCond %{HTTP_REFERER} \b(q\w*|text)(=|%3D)([%\w]*)word1(\+|%2[0B])*word2
RewriteRule blahblah\.html /paintings/refrats/otherblahblah.html [R=301,L]


.aa and .bb are the two countries I'm targeting. q (or query or similar) or text are words used by search engines. Everything involving % is from characters getting encoded in transit, like %20 for space. And finally word1word2 is the search term I'm looking for, with extra stuff in the middle because it might be one word or two.

If, after all that, they try to go to page blahblah.html, I pack them up and send them instead to a different page in the same neighborhood. (When you are small, you can amuse yourself this way ;))

The redirect ought to start with http://www.example.com/ but I can get away with leaving this out because my host does the www-redirecting, so by the time anything reaches my htaccess there's only one possible domain.

g1smd

5:35 pm on Nov 13, 2011 (gmt 0)

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



In normal operation do include the domain name in the RewriteRule target.

Without it, certain requests will have to pass through a double redirect. You should avoid that.

verbatim

6:54 pm on Nov 13, 2011 (gmt 0)

10+ Year Member



Thanks again Lucy. I find that when I use the code below it loops.


RewriteCond %{HTTP_REFERER} \.
RewriteCond %{HTTP_REFERER} !^http://www\.mysite\.com [NC]
RewriteRule (word) http://mysite.com/2011/10/19/word/ [R=302,L]


I had the same looping issue after trying the code below:

RewriteCond %{HTTP_REFERER} \.
RewriteCond %{HTTP_REFERER} \b(q\w*|text)(=|%3D)([%\w]*)word1(\+|%2[0B])*word2
RewriteRule http://mysite/best-word1-word2-blah http://mysite.com/new-blah-word1 [R=301,L]


I assume the reason is because the "word" is also in the url that it is being redirected to. Is that correct and if so how would i get around that issue?

Thanks again

lucy24

10:09 pm on Nov 13, 2011 (gmt 0)

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



Didn't someone along the line give you the boilerplate about using example.com? You can trick the Forums into displaying other urls, but the trick gets lost in copy-and-paste.

I find that when I use the code below it loops.

RewriteCond %{HTTP_REFERER} \.
RewriteCond %{HTTP_REFERER} !^http://www\.example\.com [NC]
RewriteRule (word) http://example.com/2011/10/19/word/ [R=302,L]


Time for a chorus of

It never does quite what I want
But only what I tell it.


Here, you've told it

#1 Invoke the rule only if the referer DOES NOT start with "www.example.com" without www.

#2 Send the visitor to "example.com" without www.

RewriteRule http://example/best-word1-word2-blah et cetera


I'm surprised this does anything at all. It would seem to capture only requests for

http://(www\.)?example.com/http://example/best-word1-word2-blah

Whoops! That's two different ways to break the "example" exception.

:: detour to check logs ::

Yup, the referer remains exactly the same after a Redirect. So you have to make sure your rewrite_rule only applies to the non-redirected form. Constraining it via {THE_REQUEST} won't work, since it's a redirect rather than a rewrite.

:: further detour to pore over Apache docs ::

It kinda, sorta sounds as if the leading http business is deleted anyway. We're in a directory here, right?, not the top-level config file?