Forum Moderators: phranque

Message Too Old, No Replies

Adding a query string to a subdomain

mod_rewrite query string added to url

         

GoingLoco

9:08 am on Dec 15, 2009 (gmt 0)

10+ Year Member



I must really be behind the times, because I thought WebmasterWorld was still behind a paywall :-)

Sorry that my first post is a request.

My problem: I'm trying to add a query string to a subdomain. It's one specific query string, so there's no mass regex matching or anything.

[subdomain.mysite.com...]

to

[subdomain.mysite.com...]

my last failed attempt (one of many):


Options +FollowSymlinks
RewriteEngine On
RewriteCond %{THE_REQUEST} \/index.php\ HTTP
RewriteRule index.php index.php\?id=elephant [L,R=301]

index.php is in the root of the subdomain

Any help greatly appreciated

jdMorgan

6:56 pm on Dec 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How did this fail?
Where is this code located with respect to your subdomain's files?

Be aware that no escaping is required on the "/" in the RewriteCond pattern, but that literal periods and spaces should be escaped in both the RewriteCond and RewriteRule patterns. In addition, your patterns should be start- and end-anchored when possible using "^" and "$". For example, your ruleset should be something like this:


RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php\ HTTP/
RewriteRule ^index\.php$ index.php?id=elephant [R=301,L]

The addition at the beginning of the RewriteCond pattern takes into account the HTTP method and space in the Request line -- for example, "GET ".

Unfortunately, none of these tweaks are likely to 'fix' your current problem because they just improve the 'specificity' of your patterns to prevent unexpected/undesirable operation.

Also be aware that www.subdomain.domain.com is a sub-sub-domain. You might consider sticking with the 'usual' best practices, using the 'www' subdomain or just domain.com for your main site, and subdomain.domain.com for your subdomains; I'd hate to have to read those sub-sub-domain URLs over the phone or on the radio, for example, and the www on the front end is just wasted bytes.

Jim

[edited by: jdMorgan at 9:36 pm (utc) on Dec. 15, 2009]

GoingLoco

9:24 pm on Dec 15, 2009 (gmt 0)

10+ Year Member



Thanks Jim.

The tweaks stopped the browser seizing up, but no redirection happened. I just stayed at the same page.

The .htaccess file is in the same folder as index.php

I've had a look at subdomains on other sites and they don't use the www (e.g. taxes.about.com, shop.nike.com) but would removing the www make any difference?

Is it just that realistically this is too complicated to do?

jdMorgan

9:50 pm on Dec 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, it shouldn't be complicated at all -- once we get the terminology right and the facts filled it -- and there are a lot of them to deal with at first...

Try getting rid of that backslash preceding the "?" in the RewriteRule substitution URL -- I missed it on the first pass (See the code correction in my previous post).

Next completely-flush (delete) your browser cache (remember to always do this after any change to any code on your server), and then test again.

Also, just for clarity, this code goes into the .htaccess file in the same top-level directory as the index.php file for that subdomain -- Yhat is, in the same folder as the subdomain's "home page." If the code is in the wrong location, it either won't get executed or the RewriteRule pattern won't match.

As I stated, "www.subdomain.domain.com" is a waste of bytes and typing, might confuse the search engines, and is unnecessary. It will also interfere with 'branding' your site.

While these may not seem so important now, you are at most risk if your site is successful. You may join the legion of posters in the Google forum asking, "How many months does it take Google to re-index my site and pick up all my new URLs" or "How come Yahoo! keeps trying to index my site by just the domain name and no subdomains?"

Like all of us, you will likely rely on search engines for traffic, and anything you can do to make your site straightforward and simple for them to index (i.e. avoid triggering obscure bugs in their software) will be beneficial. Run a super-tight ship on your server (e.g. use specific patterns and pattern-anchoring as I previously mentioned, force domain canonicalization, avoid duplicate-content, etc.), and preclude the majority of problems we discuss here at WebmasterWorld right from the start. Never accept sloppiness for convenience, and always think long-term (unless your site is intended to be temporary). :)

Jim

GoingLoco

10:34 pm on Dec 15, 2009 (gmt 0)

10+ Year Member



Ok, got rid of the backslash (read somewhere that it was necessary for some characters). No difference.

What I also did was try a header(location:http://subdomain.mysite.com/?id=elephant) in index.php just as a test, with the .htaccess deleted, and t the browser threw up 'Firefox has detected that the server is redirecting the request for this address in a way that will never complete'

Navigating directly to the url from the address bar works fine, however. Don't know if that means anything.

Is it because I'm trying to redirect from index.php to index.php, with only the addition of a query string?

jdMorgan

11:08 pm on Dec 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your .htaccess code is correct, in that it checks "THE_REQUEST" to be sure that the query string is blank before adding the 'elephant' query and invoking a redirect. Your PHP script failed because it didn't contain that test, most likely.

So the question remains, "Why doesn't your .htaccess code get executed, and if not, why not?"

Try a simpler rule, just as a test:


Options +FollowSymLinks
RewriteEngine on
#
RewriteRule .* http://www.google.com/ [R=301,L]

This has no dependencies on the requested URL-path or query string, and should redirect all incoming requests to google.com.

If that works, then there's some detail of the URL-path or query string that's not right in your original code.

Jim

GoingLoco

8:01 am on Dec 16, 2009 (gmt 0)

10+ Year Member



That worked perfectly. Therefore I'll go over the url-path / query string and see where my error is.

GoingLoco

12:40 pm on Dec 17, 2009 (gmt 0)

10+ Year Member



Thanks, jdMorgan for your help. In the end I figured out a simple way to work around the problem.

It seemed to be a subdomain thing, because the same script in the root domain with a redirect worked perfectly.

Thanks once again.