Welcome to WebmasterWorld Guest from 34.207.78.157

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

redirects with parameters not working

     
8:18 pm on Jul 13, 2016 (gmt 0)

New User

10+ Year Member

joined:Oct 5, 2007
posts: 18
votes: 0


Ok guys, add me to a list of 9 million other ppl who are confused by htaccess rules, and want to do "one simple thing" lol... Which of course isn't as simple as the ignorant think it is. I will appreciate ANY assistance or pointers you can give.

I have several urls like this... but these redirect lines don't work, because I have parameters...
Redirect 301 http://example1.com/cart/index.php?cPath=42_65 http://example2.com/index.php/belay-devices/assist-lock-devices.html
Redirect 301 http://example1.com/cart/index.php?cPath=42_67 http://example2.com/index.php/belay-devices/belay-plates-tubes.html
Redirect 301 http://example1.com/cart/index.php?cPath=42_64 http://example2.com/index.php/belay-devices/figure-8-s.html

I went thru the interface in my hosting, and made one, and it looked like this:
RewriteCond %{HTTP_HOST} ^example1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example1\.com$
RewriteRule ^cart\/index\.php$ "http\:\/\/example2\.com\/index\.php\/belay\-devices\/belay\-devices\/assist\-lock\-devices\.html" [R=301,L]


But again, it doesn't account for the parameters.

So, I changed it to this and it works
RewriteCond %{HTTP_HOST} ^example1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example1\.com$
RewriteCond %{QUERY_STRING} cPath=42_65
RewriteRule ^cart\/index\.php$ "http\:\/\/example2\.com\/index\.php\/belay\-devices\/belay\-devices\/assist\-lock\-devices\.html" [R=301,L]


The first one works, but the next one doesn't, nor do the rest, I end up with 404 errors.
RewriteCond %{HTTP_HOST} ^example1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example1\.com$
RewriteCond %{QUERY_STRING} cPath=42_65
RewriteRule ^cart\/index\.php$ "http\:\/\/example2\.com\/index\.php\/belay\-devices\/belay\-devices\/assist\-lock\-devices\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^example1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example1\.com$
RewriteCond %{QUERY_STRING} cPath=42_67
RewriteRule ^cart\/index\.php$ "http\:\/\/example2\.com\/index\.php\/belay\-devices\/belay\-plates\-tubes\.html" [R=301,L]
RewriteCond %{HTTP_HOST} ^example1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example1\.com$
RewriteCond %{QUERY_STRING} cPath=42_64
RewriteRule ^cart\/index\.php$ "http\:\/\/example2\.com\/index\.php\/belay\-devices\/figure\-8\-s\.html" [R=301,L]


I strongly suspect it's in this part:
RewriteRule ^cart\/index\.php$ 
8:26 pm on July 13, 2016 (gmt 0)

New User

10+ Year Member

joined:Oct 5, 2007
posts: 18
votes: 0


ok, I see now that the rule kind of ignores the parameters, which I want to throw out anyway.

What I want it to say is
if it's example1.com or www.example1.com AND the parameter is this, then rewrite the entire url to this new url. I think I should be using something like (*) in it instead?

More info. No other folders will be redirected, just cart. No other pages will be redirected, just index.php. Can I just put that part in the conditions, and then test for the parameter, as the only difference? and then how would I form the rule? (*)
9:39 pm on July 13, 2016 (gmt 0)

New User

10+ Year Member

joined:Oct 5, 2007
posts: 18
votes: 0


Ok, still researching
it appears when I say "throw away the old url" (to send to the new url) I should write it like this:
^(.*)$
But that still doesn't explain why the rest aren't working. As I look back thru the code as posted in the post, I see the color coding seems broken... that would mean my code is not right at the end of the www condition?
10:04 pm on July 13, 2016 (gmt 0)

New User

10+ Year Member

joined:Oct 5, 2007
posts: 18
votes: 0


Ok, this is what I figured out that works:
RewriteCond %{HTTP_HOST} ^example1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example1\.com$
RewriteCond %{QUERY_STRING} ^cPath=42_65
RewriteRule ^(.*)$ "http\:\/\/example2\.com\/index\.php\/belay\-devices\/belay\-devices\/assist\-lock\-devices\.html" [R=301,L]


The changes are in the 3rd condition, the query string, I added the carat in from of the string I was looking for, and in the rule I changed RewriteRule ^cart\/index\.php$ to ^(.*)$

I hope my struggles and conclusion help someone else.
10:06 pm on July 13, 2016 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 2, 2006
posts:2240
votes: 8


RewriteCond %{HTTP_HOST} ^example1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example1\.com$
RewriteCond %{QUERY_STRING} cPath=42_65
RewriteRule ^cart\/index\.php$ "http\:\/\/example2\.com\/index\.php\/belay\-devices\/belay\-devices\/assist\-lock\-devices\.html" [R=301,L]


I'm the one that usually asks, but let me try helping here:

1. You keep repeating the first two lines for your main domain, and in your RewriteRule you use "L" which means last rule, a directive to the system to stop looking further.
2. If example1.com is the only domain you use, why having it as a condition? Just go by QUERY_STRING and you should be fine.

I hope this helps.

Cheers!
11:42 pm on July 13, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15636
votes: 795


RewriteCond %{HTTP_HOST} ^example1\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example1\.com$

What's this for? Will this htaccess file be seen by requests for (a) other domains or (b) subdomains of example.com? If no to both, the condition isn't needed at all. If yes to one or both, there are alternative wordings. In no case would it need to be two lines, since they can be collapsed into
^(www\.)?example\.com
and you definitely don't need the closing anchor, since the only thing it could possibly exclude would be requests with appended port number-- and you'd want to redirect those too.

RewriteRule ^cart\/index\.php$ "http\:\/\/example2\.com\/index\.php\/belay\-devices\/belay\-devices\/assist\-lock\-devices\.html" [R=301,L]

Urk. I do believe you've been getting information from bad sources. You do not need to escape anything in the target. Ever. Under any circumstances. You also don't need quotation marks. They may even break the redirect.

In the pattern, you do not need to escape / slashes. (There are one or two mods where the slash has a RegEx-related meaning, as it does in javascript, so it has to be escaped, but mod_rewrite is not one of them.) Or--in general--anything else except literal periods.

If one purpose of the redirect is to get rid of a query string, then you need a ? at the end of the target. (In Apache 2.4 you can instead use the QSD flag, but why bother with four bytes when one will do.)

Incidentally, that's a pretty messy-looking target. "index.php" followed by more stuff in the path? Is that really what you want?

If you haven't already done so, change any existing Redirect(Match) rules to mod_rewrite syntax so you can be sure of things happening in the intended order.

in your RewriteRule you use "L" which means last rule, a directive to the system to stop looking further

That's standard in RewriteRules, because the R flag doesn't imply L (as F and G do). Only in the rarest of cases would a RewriteRule not have an L flag. It doesn't mean "stop everything"; it just means "stop rewriting for now, and go on to the next mod".

[edited by: phranque at 11:43 am (utc) on Jul 27, 2016]
[edit reason] edit of regex typo requested by lucy24 [/edit]

5:32 pm on July 26, 2016 (gmt 0)

New User

10+ Year Member

joined:Oct 5, 2007
posts: 18
votes: 0


Thanks guys for trying to help educate me and others with this issue.
Both of you asked if this was the only domain name that will see this htaccess file, and no it won't be. It's part of a multi site install of magento, with 5 stores, and several domains simply pointing at this as well. So, I still need to put the domain name lines in, right? I see they can be shortened to one line - ^(www\.)example\.com

Yes Lucy, I probably have been given bad info, but mostly because I started by going in to my hosting interface and creating a redirect there. This redirect didn't handle the parameters tho, so I had to manually "fix" them. The redirect put the index.php in there at the begining, but I dropped it when I realized I didn't need it in the rewrite rule.

The original url was ugly. [example1.com...] The only info I need to know is the value of that parameter, the rest of the url will always be exactly the same.

The actual url of the page I want them to end on includes the index.php, so I believe I have to keep it. [example2.com...]

So, This is what I think you're saying I'd end up with:
RewriteCond %{HTTP_HOST} ^(www\.)example\.com 
RewriteCond %{QUERY_STRING} ^cPath=42_65
RewriteRule ^(.*)$ http://example2.com/index.php/belay-devices/belay-devices/assist-lock-devices.html [R=301,L]
2:22 am on July 27, 2016 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

joined:Apr 9, 2011
posts:15636
votes: 795


I had a minor brain fart up above (have asked mods to fix it). Should be
^(www\.)?example\.com
with question mark, meaning that the www is optional. But if you're only checking for domains, not for subdomains, you can simply say
example\.com
with no anchors at all. Unless, perish the thought, your domains have overlapping names.

For future reference: If you need to discuss more than one domain, you can say example.org, example.net, example.uk, example.xyz and so on. But it has to be example.

RewriteRule ^(.*)$

The anchors are superfluous, because a Regular Expression is greedy by default. But note that a pattern of this form will match ALL requests, including things like images that can't possibly ever have this parameter. You should constrain the body of the rule to paths that might potentially match, so the server doesn't have to evaluate conditions on every single request ever. So if the only possible path is
^cart/$
then use that. Or at least ^cart/ if you have doubts about the rest. (You cannot put it past a search engine to request "example.com/cart/index.html" just to see what happens.)
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members