Forum Moderators: phranque

Message Too Old, No Replies

www rewrite without stripping URL parameter

www rewrite without stripping URL parameter

         

iSeeHow

9:20 pm on Jul 12, 2012 (gmt 0)

10+ Year Member



I have the following rewrite rule that adds "www" to the domain.

RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com [R=301,L]

But we have a mobile site (m.example.com) that has a "desktop" link that points to http://www.example.com/?nomobile=1

The problem is my current rewrite strips the URL parameter and the desktop link doesn't work on our mobile site.

[edited by: incrediBILL at 10:33 pm (utc) on Jul 20, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

phranque

11:09 pm on Jul 12, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, iSeeHow!

that RewriteCond will fail for the requested host, so which RewriteRule is being invoked that strips your query string?

also please read the pinned post in this forum about using Example.com For Domain Names in Posts:
http://www.webmasterworld.com/apache/4452736.htm [webmasterworld.com]

g1smd

11:53 pm on Jul 12, 2012 (gmt 0)

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



I have the following rewrite rule that adds "www" to the domain.

That isn't quite what it does. Requests for any hostname other than "exactly" www.example.com are redirected to www.example.com preserving the same path in the new request.


To fix the "mobile problem"; in the same way as the existing "NOT www" condition works, add another RewriteCond for "NOT mobile site".

Escape all literal periods in patterns.

^(.*)$ simplifies to (.*)

Use example.com in this forum.

lucy24

1:47 am on Jul 13, 2012 (gmt 0)

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



The problem is my current rewrite strips the URL parameter

There's something you're not telling us. By default, rules using mod_rewrite simply ignore any query string: They stash it in a safe place, do their stuff, and then reappend the query unchanged. This applies equally to redirects (R flag) and rewrites.

You can search this Forum for "query string boilerplate" and learn much much more than you ever wanted to know.

iSeeHow

11:50 am on Jul 13, 2012 (gmt 0)

10+ Year Member



Thanks for welcoming me, I will also use "example.com" from here on out.

glsmd: I thought that is what's happening but I wasn't sure of the syntax. I tried to the best of my limited knowledge of rewritecond but it's not working for me. All my rewritecond rules:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /example.com/

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteCond %{HTTP_HOST} !^www.example.com/?nomobile=1
RewriteRule ^(.*)$ http://www.example.com [R=301,L]

phranque

11:58 am on Jul 13, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



the query string will not appear in the HTTP_HOST environment variable.

iSeeHow

12:01 pm on Jul 13, 2012 (gmt 0)

10+ Year Member



So is the fix just removing that variable? ie:
RewriteCond !^www.example.com/?nomobile=1

iSeeHow

12:06 pm on Jul 13, 2012 (gmt 0)

10+ Year Member



Apparently not, I just tested that. I'm going to search "query string boilerplate" and see what wealth of knowledge I find there as I obviously need to learn a little bit subject manner.

g1smd

1:14 pm on Jul 13, 2012 (gmt 0)

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



You're redirecting "not www.".
You need to also test for "not m.".

iSeeHow

1:36 pm on Jul 17, 2012 (gmt 0)

10+ Year Member



Can someone please link to the boiler plate article, it's mentioned a lot but it's kind of hard to find. Thanks.

lucy24

11:07 pm on Jul 17, 2012 (gmt 0)

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



Aw, heck, I'll just paste it in again. It's been a few weeks.

Query Strings

The Query String, also known as a Parameter, is the part of an url after the question mark. Question = query.

By default, rewrites simply ignore the query string. That is, mod_rewrite stashes the query in a safe place, does its stuff to the part before the question mark, and then reappends the original query.

Changing a Query

#1 To delete a query, add a ? to the end of your rewrite target.
#2 To replace a query—or create a new one—add ?blahblah to the rewrite target. The blahblah can be either literal text, or stuff you captured earlier. (#1 and #2 are really the same thing: you're just replacing the query with either something or nothing.)
#3 To add to an existing query, again put ?blahblah at the end of the target, but also add [QSA] to your flags (the bracketed items at the end of the Rule). It stands for "Query String Append", meaning that the blahblah is to be added to the existing query—if any—instead of replacing it.

Getting the Query

You only need to retrieve the original query if
#1 you want the rewrite to behave differently depending on what the query was
or
#2 you need to change or delete the query

Add a Condition that says

RewriteCond %{QUERY_STRING} blahblah


using your ordinary Regular Expressions, anchors and ! as needed.

To test whether there was a query at all

RewriteCond %{QUERY_STRING} .


which simply means "If the query contains at least one character of any kind".

If you need to capture any of the query, use parentheses as usual. In the rewrite target, the captures will be %1, %2 etc instead of $1, $2 etc, because they are coming from a Condition instead of the Rule. Each set is separately numbered, so the first capture from the Rule will still be $1.

iSeeHow

12:37 pm on Jul 18, 2012 (gmt 0)

10+ Year Member



Thanks. That helped a lot. Should I still use the R=301,L flags as well? Or do I not need the 301?

lucy24

10:59 pm on Jul 18, 2012 (gmt 0)

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



Yes, always use 301. R alone defaults to 302. This is rarely what you want. And you do need the [L] flag with redirects. Unless, ahem, your name is jdmorgan and you really don't want the rewrites to stop there. I know it's counter-intuitive, but a Redirect doesn't automatically imply [L] the way [G] or [F] does.