Page 1 of 1

[RegEx] - ReWrite rule

Posted: Thu Jun 28, 2012 6:11 pm
by gscrmn
Hello folks!

This is my first post here and I need some help regarding RegEx.

I need to make a Rewrite rule. Basically I have 3 situations:

1) www.mydomain.com
2) http://www.mydomain.com/?utm_source=sou ... aign=blogs (Google URL Builder)
3) www.mydomain.com/blog or /chat, etc

With situations 1 and 2 I need the rewrite rule to be active. But with situation 3, I don't want a redirect.

So, I was wondering if this is correct:

1) ^(http[s]?://)?(www\.)?(mydomain\.com[/]?)$
2) ^(http[s]?://)?(www\.)?(mydomain\.com[/]?)$([?]utm_source[=])
3) Nothing

Is that right? Is this secure? Okay?

Or there is any other way to put just 1 single rule for all 3 situations?

Thanks a lot in advance!!!!!

Re: [RegEx] - ReWrite rule

Posted: Thu Jun 28, 2012 6:12 pm
by gscrmn
Oh... I almos forget!

In situation 2, I need that the parameters (utm_source, etc) to go to the next page, so I can handle them

So, I'm almost sure that I made it wrong, I miss something, right?

Thanks again!

Re: [RegEx] - ReWrite rule

Posted: Fri Jun 29, 2012 12:40 am
by requinix
And where are those redirects to?

Re: [RegEx] - ReWrite rule

Posted: Fri Jun 29, 2012 7:12 am
by gscrmn

Re: [RegEx] - ReWrite rule

Posted: Fri Jun 29, 2012 9:58 pm
by requinix
Does 1 require "the query string is empty"? If not then you can forget about 2 because 1 covers it. Then all you're doing is redirecting away from the homepage.
Otherwise you're still redirecting from the same page, you just need two RewriteRules each with a RewriteCond for the query string.

Oh, and RewriteRules do not process the query string. Only the path and filename.

Re: [RegEx] - ReWrite rule

Posted: Mon Jul 02, 2012 7:16 am
by gscrmn
What I want to do is that when the user typer mydomain.com OR mydomain.com/utm_source=source he goes to mydomain.com/pub/index.htm

But, if typer mydomain.com/chat or mydomain.com/blog, he goes to the specific page

And, when the page is mydomain.com/utm_source=source I need to get the info after utm_source

Thanks a lot!

Re: [RegEx] - ReWrite rule

Posted: Mon Jul 02, 2012 11:17 pm
by requinix
You can use a RewriteCond to look at the query string, then pair that with a RewriteRule (that only looks at the path/file) to redirect. Like

Code: Select all

# contains utm_source
RewriteCond %{QUERY_STRING} (^|&)utm_source=([^&]+) [OR]
# it's empty
RewriteCond %{QUERY_STRING} ^$
# QSA=pass all the query string arguments in the new URL
RewriteRule ^$ /pub/index.htm [L,QSA]
You might want an [R] in there, I'm not sure.

Re: [RegEx] - ReWrite rule

Posted: Tue Jul 03, 2012 3:14 pm
by gscrmn
Thanks a lot for your answer requinix, but as I'm a newbie, a don't know if I want a [R] there, because I don't know what it does.

Is it possible to you to make the cond and the rewrite rules?

I ask this because I really do not have a clue on this.

No idea how to get where I want ;(

Thanks a lot in advance!

Re: [RegEx] - ReWrite rule

Posted: Tue Jul 03, 2012 9:08 pm
by requinix
gscrmn wrote:as I'm a newbie, a don't know if I want a [R] there, because I don't know what it does.
Normally URL rewriting works behind-the-scenes: the browser doesn't know about the redirect. Using an [R] (for redirect) means the browser is told where to go instead. That also means the user sees the URL change to the new location. And also means it's a lot less likely that people will continue going/clicking links to the old URL.
gscrmn wrote:Is it possible to you to make the cond and the rewrite rules?
...Again?

Re: [RegEx] - ReWrite rule

Posted: Wed Jul 04, 2012 1:09 pm
by gscrmn
Thanks a lot again requinix, I'm almost there.

So if I add this instructions:

RewriteCond %{QUERY_STRING} (^|&)utm_source=([^&]+) [OR]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^$ /pub/index.htm [L,QSA]

When a user types:
1) http://www.mydomain.com - He will be redirect to http://www.mydomain.com/pub/index.htm
2 http://www.mydomain.com/?utm_source=... - He will be redirect to http://www.mydomain.com/pub/index.htm?utm_source=...
3) http://www.mydomain.com/chat or /blog or /anythingelse - No redirect happens

Is that correct?

And 2 other doubts:

1) Will the visitor "visit" my index.php at the root directory or the redirect happens BEFORE this?
2) I want the page to ALWAYS redirect, this will be a permanent redirect. So, in your opinion, is better to put a R?

Thanks again!!!

Re: [RegEx] - ReWrite rule

Posted: Wed Jul 04, 2012 5:35 pm
by requinix
gscrmn wrote:So if I add this instructions:

RewriteCond %{QUERY_STRING} (^|&)utm_source=([^&]+) [OR]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^$ /pub/index.htm [L,QSA]

When a user types:
1) http://www.mydomain.com - He will be redirect to http://www.mydomain.com/pub/index.htm
2 http://www.mydomain.com/?utm_source=... - He will be redirect to http://www.mydomain.com/pub/index.htm?utm_source=...
3) http://www.mydomain.com/chat or /blog or /anythingelse - No redirect happens

Is that correct?
1. Try it and see.
2. Given my explanation of what [R] does, do you think you want it?
gscrmn wrote:1) Will the visitor "visit" my index.php at the root directory or the redirect happens BEFORE this?
Before.
gscrmn wrote:2) I want the page to ALWAYS redirect, this will be a permanent redirect. So, in your opinion, is better to put a R?
I never said anything about (vanilla) [R] being a permanent redirect. If you want that then the syntax would be [R=301].

Re: [RegEx] - ReWrite rule

Posted: Thu Jul 05, 2012 1:01 pm
by gscrmn
1. Try and see is the worst option, because if it fails, my entire website will not be functional, so it is definetly not a good idea. This is the main reason why I'm here: to ask for help. To try and see, I could do a basic Google search. I need to be sure that it will work.
2. I don't got your explanation about [R], sorry. Lot less likely that people will continue going/clicking links to the old URL is worthless, because I need people keep going on that "link" because it is my MAIN DOMAIN. And does not make difference if the user sees the URL changing.

I know you never said anything about (vanilla) [R] being a permanent redirect. But I want it to be, because it is not temporary, it is forever. Every time that someone type my web address, it has to be redirect.

But thanks for your cooperation. You where the only one that always give me answers. At all!

Re: [RegEx] - ReWrite rule

Posted: Thu Jul 05, 2012 9:17 pm
by requinix
gscrmn wrote:1. Try and see is the worst option, because if it fails, my entire website will not be functional, so it is definetly not a good idea.
Then thank God that you write perfect, 100% bug-free code, because if there ever was a flaw then all your users could see it!
gscrmn wrote:This is the main reason why I'm here: to ask for help. To try and see, I could do a basic Google search. I need to be sure that it will work.
Help is what I've been giving you, but in return I expect you to try to help yourself. You don't quite understand [R] yet? Okay: where else have you looked for information? The mod_apache documentation? How about a basic Google search?

And one more thing: if you think the information you get on the Internet will always work then you don't understand what the Internet is. Not only are you trusting a bunch of people you don't know, you don't even have some way of testing what they've said. What if your setup is unusual? If you don't have mod_rewrite installed then even a perfect example of how to use RewriteRule would crash your site.
gscrmn wrote:2. I don't got your explanation about [R], sorry. Lot less likely that people will continue going/clicking links to the old URL is worthless, because I need people keep going on that "link" because it is my MAIN DOMAIN. And does not make difference if the user sees the URL changing.
Okay. So the generic information I gave doesn't quite apply entirely to your specific situation.
gscrmn wrote:I know you never said anything about (vanilla) [R] being a permanent redirect. But I want it to be, because it is not temporary, it is forever. Every time that someone type my web address, it has to be redirect.
Great. Then it's good that I pointed it out before you came back and told me it wasn't working.

Re: [RegEx] - ReWrite rule

Posted: Fri Jul 06, 2012 2:20 pm
by gscrmn
What I have to say is I'm sorry!

You are ABSOLUTELY right, 100%!

I'm so sorry about what I said, you have the reason when you say about test and help.

You just tried to help, but I'm a little ancious to have it working, because I'm trying to make it functional about 10 days and did not got it!!!!!

But thank you and shame on me!