Forum Moderators: phranque

Message Too Old, No Replies

url redirection

url redirection

         

thosecars82

2:57 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



Hello
I have this problem and I am kind of desperate with it.
I would like to redirect any url in this pattern www\.example\.com/writeEmail\.php?(.*) to
http://www.example.com/writeEmail/language/sp/
Actually, I almost have it done because the string
http://www.example.com/writeEmail.php is redirected successfully. Nonetheless,
as for the other strings which match the pattern described above, they add an extra part which I do not want at the end of the redirected string. In fact they are redirected like this:

http://www.example.com/writeEmail? -> http://www.example.com/writeEmail/language/sp/?
http://www.example.com/writeEmail?abc -> http://www.example.com/writeEmail/language/sp/?abc
http://www.example.com/writeEmail?234 -> http://www.example.com/writeEmail/language/sp/?234
....
Do you have any suggestion to get that those redirections work like I want, that is to say:

1st: http://www.example.com/writeEmail? -> http://www.example.com/writeEmail/language/sp/
2nd: http://www.example.com/writeEmail?abc -> http://www.example.com/writeEmail/language/sp/
3rd: http://www.example.com/writeEmail?234 -> http://www.example.com/writeEmail/language/sp/

I just tried with these two ways to at least make the first of these redirections examples work and neither of these ways worked so far:

RewriteCond %{REQUEST_URI} writeEmail\?
RewriteRule .* writeEmail/language/en/ [NC]

and the other way:
RewriteRule ^www\.example\.com/writeEmail\?$ www\.example\.com/writeEmail/language/en/ [NC]

The only think I might think of is that other rules I have might be interfering with this one.
Any suggestion would be appreciated.

[edited by: jdMorgan at 3:32 pm (utc) on Oct. 9, 2008]
[edit reason] No URLs, please. Use example.com only. [/edit]

jdMorgan

3:31 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The question mark is not part of the URL. Neither is it part of the query string. Rather, it is a delimiter between the two.

The simplest way to get rid of it is to add a blank query string to the substitution URL. You must also anchor your "writeEmail" pattern to avoid recursion, and use the [L] flag for efficiency:


RewriteRule [b]^w[/b]riteEmai[b]l$[/b] writeEmail/language/e[b]n/?[/b] [NC[b],L[/b]]

Jim

thosecars82

4:46 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



Thank you very much jdMorgan. It worked like you said at the first try. It is good to know that the question mark is neither in the url nor in the query.

thosecars82

7:27 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



Please
let me have this clear. In this example:
http://www.example.com/a/b?c=4
URI=www.example.com/a/b?c=4
URL=www.example.com/a/b
query string=c=4

Question1: Am I right?

As for a rewriterule like
RewriteRule Pattern Substitution

Question2: is the pattern only applied to the url and not to the query string?

Thanks

jdMorgan

7:48 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



URI and URL are the same thing if you're talking about HTTP. So in both cases, the URL/URI is
URL = www.example.com/a/b

and as you surmise,
query string = c=4

Remember that the purpose of a URL or URI is to "locate" a resource -- a page, a script, a photo, etc. on the Web. In no case does the query string represent a "location" on the Web. It may indicate the location of a record in a database on a server, but it does not serve to locate anything on the Web, since the URL is the thing that locates the script that gives that query string or database entry meaning.

So in all cases, query strings are treated as data *attached* to a URL, to be passed to the resource *at* that URL.

In mod_rewrite the query string is not "seen" by RewriteRule, and it is not part of the REQUEST_URI variable.

Unless a new query string is present in the RewriteRule substitution, any existing query string will pass through mod_rewrite unchanged. You can clear the existing query string (as we did above), replace it with a new one, or append more variables to the existing ones (see RewriteRule [QSA] flag).

In order to evaluate the query string, you must use


RewriteCond %{QUERY_STRING} [i]pattern[/i]

Jim

thosecars82

8:36 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



Hi jdMorgan, you are being very helpful indeed.
Now I wonder why these two sentences
RewriteCond %{REQUEST_URI} writeEmail$
RewriteRule ^writeEmail$ /writeEmail/language/en/ [NC,L]

do not have the same behavior as what you wrote above, that is to say:
RewriteRule ^writeEmail$ writeEmail/language/en/? [NC,L]

Any idea?
Thanks

thosecars82

8:39 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



When I said the same behavior I meant that I do not understand why the first couple of sentences does not get to remove the query string.
Thanks

jdMorgan

9:04 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Because there is no "?" in the substitution URL, and that is required in order to replace the existing query string with a "blank" one.

Jim

thosecars82

10:00 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



What would be the difference in behavior between using

RewriteRule ^writeEmail$ writeEmail/language/en/? [NC,L]
and using
RewriteRule ^writeEmail$ writeEmail/language/en/? [NC,L,R=301]
It is just that firstly I thought that placing R=301 in the flags section was the thing that made that change in the url entered by the user was visible. However, now I realized that the change in the url entered by the user is visible without setting R=301 as a flag. Therefore my question is: what is the purpose of R=301?
Thanks

g1smd

10:06 pm on Oct 9, 2008 (gmt 0)

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



Subtle changes in syntax change a line of code from being a "301 redirect to another URL" to a "302 redirect to another URL" to a "rewrite to an internal filepath".

It is important to be aware of the differences.

The change of URL being visible when the [R] is not present, is probably caused by having a redirect later in the code, and that redirect then exposes the internal filepath. Redirects should normally be listed before rewrites.

[edited by: g1smd at 10:08 pm (utc) on Oct. 9, 2008]

jdMorgan

10:07 pm on Oct 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



[R=301] sends a response to the client (browser or robot) saying, "The object you requested has moved permanently, ask for it again at this new address."

This does not "change" the URL, it replaces it. The browser has to ask for what it wanted a second time, using the URL supplied in the redirection response. The only way to prevent this "two requests to get what it wanted" redirection is to correct the URL displayed or linked-to on your original page -- The link the user clicks on.

So, the question is "what is the URL you want listed in search engines?"

Put that URL on each page (usually by modifying the script that produces your on=page links).
Then add mod_rewrite code to "re-connect" that URL to the correct filepath on your server.
Finally, add a redirect from the old URL to the new URL, but only when the old URL is requested by a client.

Jim

thosecars82

10:07 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



It is just because it seems to me that it might be desirable to place R=301 all the times that we placed the L flag. Nonetheless, if there is some example in which it would be recommended to place only the L flag I would be interested in knowing what situation would be that.
Thanks
PD:Sorry for writing the exact domain in the first post.

g1smd

10:10 pm on Oct 9, 2008 (gmt 0)

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



When using [L] on its own, you are normally coding a rewrite: connecting a URL request to an internal server path and file.

When you use [R] (with or without a number, like 301 or 302) you are coding a redirect to a URL - and that rule should include a full URL, with http method and domain name included.

When coding a rewrite, the http and domain name will *not* be present - just the folder and filepath pointing to the location of the content on the server.

[edited by: g1smd at 10:13 pm (utc) on Oct. 9, 2008]

thosecars82

10:12 pm on Oct 9, 2008 (gmt 0)

10+ Year Member



After previous post I saw that you had already replied, sorry.
I think I kind of understand now. It is just that I started messing with this rewrite rules because I thought it would be useful to avoid being penalized by google for aliases. Google had links to my pages with dynamic urls which I already fixed to make them static by some rewrite_mod tricks. So what I wanted now was just to get that Google changed those dynamic links for the new static ones. So I guess, I am right in this case if I choose to set the flag R=301 to get that Google forgets about the old dynamic urls, right?
Thanks jdMorgan

g1smd

10:15 pm on Oct 9, 2008 (gmt 0)

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



Yes. The redirect says "forget the old URLs - go fetch the stuff over there at the new location".

You'll want that to return the 301 code in the HTTP header, hence the [R=301] in the rule.

If you just put [R], then you end up with a 302 redirect and that is often not what you want to do.

This is heavy stuff, but if you just read the comments on the code in this example [webmasterworld.com...] and see the overall process, then that is a very good start.

thosecars82

5:09 pm on Oct 10, 2008 (gmt 0)

10+ Year Member



Thank you. That link you gave me seems pretty good.