Forum Moderators: phranque
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]
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]]
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
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]
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
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
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]
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
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]
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.